2013-08-04 75 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