从链接href更改iframe源

时间:2013-08-04 07:40:30

标签: javascript jquery

我需要更改来自<a>网址的iframe来源。 http://jsfiddle.net/YkKu3/

我的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
        });    

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

您将事件的顺序混淆了。正确的代码将锚点URL加载到iframe中:

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

Live test case。 (带着可爱的猫咪:))