2013-08-04 78 views
0

我需要从<a> url更改iframe源。 http://jsfiddle.net/YkKu3/从链接更改iframe源href

我的HTML:。

<a class='gotothis' href='http://puu.sh/666.png'>this Image</a> 
<button type="button">Click Me!</button> 
<iframe id="frameimg" src="" width="300" height="300"> 
</iframe> 


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

你可以把一些更多的灯光,你想要什么 –

回答

1

您已将事件顺序混淆。正确的代码,将锚网址加载到iframe中:

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

Live test case。 (用一只可爱的猫咪:))

+0

**谢谢**先生,工作 – flywithlovepls

3

您使用的是双引号“打开字符串,但一个单引号'将其关闭或者使用两个双引号或两个单引号

$("#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 
+0

的确。事实上,下列代码中的所有代码都有“这是一个字符串”的语法突出显示,应该是一个提示。尝试了 – GolezTrol

+0

。不工作http://jsfiddle.net/YkKu3/请帮助 – flywithlovepls

+0

即时对不起,它的工作,谢谢 – flywithlovepls