【问题标题】:load link src into iframe with jquery使用 jquery 将链接 src 加载到 iframe 中
【发布时间】:2012-07-09 22:16:51
【问题描述】:

我想知道是否可以让 jQuery 查看 div 类中的一组链接,当单击其中一个链接时,该链接的 src 将加载到 iframe 中。

我想应该是这样的

$(function() {
$('.link_group a').on('click', function(){
   $('iframe#frameID').attr('src', $(this).attr);
});
});

编辑——我的 jQuery 现在看起来像这样

$(function() {
$('.link_group a').on('click', function(){
$('iframe#frameID').attr('src', $(this).prop('href'));
});
});

HTML

<div class="link_group">
<a href="Link 1.asp">Link 1</a>
<a href="Link 2.asp">Link 2</a>
<a href="Link 3.asp">Link 3</a>
<a href="Link 4.asp">Link 4</a>
</div>

但那绝对行不通..

有人知道如何正确执行此操作吗?

【问题讨论】:

    标签: jquery iframe src


    【解决方案1】:

    您必须将参数传递给attr() 方法,并且还要选择哪个属性:

    $(this).attr('href')
    

    或者,最近在 jQuery 中:

    $(this).prop('href');
    

    此外,id 在文档中是唯一的,因此除非您在其他页面中使用完全相同的脚本并在其他元素类型上使用该 id,否则指定 iframe 是多余的,并且可能会减慢您的速度选择器。

    【讨论】:

    • 感谢您的回复。我在我的函数中尝试了$('iframe#frameID').attr('src', $(this).prop('href'));,但也无法让它工作。我还尝试按照你的建议删除#frameID,但仍然没有运气......
    • 除了使用 prop() 之外,我还需要防止默认值。现在everything完美运行。谢谢!
    【解决方案2】:

    尝试:

    $(this).attr("href")
    

    或者最好在 jQuery 1.6+ 中:

    $(this).prop("href")
    

    代替:

    $(this).attr
    

    【讨论】:

    • ... 或者最好是 .prop(),因为 jQuery 1.6。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多