从链接href更改iframe源

我需要从 url更改iframe源代码。 http://jsfiddle.net/YkKu3/

我的HTML:

 this Image    $('button').click( function(event) { var clicked = $(this); $("#frameimg").attr("src", " .gotothis image is here, help '); // Here the problem }); 

你有混合事件的顺序。 正确的代码将锚点URL加载到iframe:

 $(window).load(function() { $('button').click( function(event) { var clicked = $(this); window.setTimeout(function() { $("#frameimg").attr("src", $(".gotothis").attr("href")); }, 1000); }); }); 

现场测试案例 。 (带着可爱的猫:))

您使用双引号“打开字符串,但单引号”关闭它。要么使用两个双引号,要么使用两个单引号。

 $("#frameimg").attr("src", " .gotothis image is here, help "); // Here the problem 

或者如果你指的是想要动态读取gotothis uri的事实,你应该从gotothis元素中读出href属性:

 $("#frameimg").attr("src", $('.gotothis').attr('href')); // Here there is no problem