gpt4 book ai didi

javascript - 当 iframe 名称是动态时的目标 iframe

转载 作者:行者123 更新时间:2023-12-02 23:38:33 24 4
gpt4 key购买 nike

我的问题是针对一个 iframe,该 iframe 具有从 jquery 添加的动态名称。看来当我尝试动态添加名称到 iframe 时,针对 iframe 的链接会在新窗口中打开。

测试代码很简单:

html:

<a href="https://www.gaugeonline.com/" target="iframe1">test</a>
<iframe src="https://test.com"></iframe>

jquery:

$('iframe').attr('name', 'iframe1');

我做了一个简单的 fiddle :https://jsfiddle.net/2ztg7xpj/1/

我还尝试动态添加目标和名称:

html:

<a href="https://www.gaugeonline.com/">test</a>
<iframe src="https://test.com"></iframe>

jquery:

$('iframe').attr('name', 'iframe1');
$('a').attr('target', 'iframe1');

和 fiddle :https://jsfiddle.net/2ztg7xpj/2/

所以问题是,为什么这是动态添加 iframe 名称的行为,是否有某种解决方法(除了直接将名称添加到 iframe 之外)

最佳答案

我仍然对默认行为感到困惑。似乎是避免弹出窗口拦截器的新方法。

无论如何,我创建了自己的解决方法。

$('a[target=frame1]').click( function( e ) {
e.preventDefault();
var href= $(this).attr('href');
$('iframe[name=frame1]').attr('src', href);
});

关于javascript - 当 iframe 名称是动态时的目标 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56190459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com