【问题标题】:Change iframe source from link href从链接 href 更改 iframe 源
【发布时间】:2013-08-04 23:14:47
【问题描述】:

我需要从 <a> url 更改 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
        });    

【问题讨论】:

  • 你能更清楚地说明你想要什么

标签: javascript jquery


【解决方案1】:

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

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

【讨论】:

  • 确实如此。以下行中的所有代码都突出显示“这是一个字符串”语法这一事实应该是一个提示。
【解决方案2】:

你把事件的顺序搞混了。将锚 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。 (和一只可爱的猫:))

【讨论】:

    猜你喜欢
    • 2014-03-24
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 2023-04-01
    • 2011-01-13
    • 1970-01-01
    相关资源
    最近更新 更多