这是 iframe 代码:
<div id="OutDiv" class="outerdiv">
<iframe src="http://beta.sportsdirect.bg/checkout/onepage/" id="InnerIframe" class="FrameCSS" scrolling="no"></iframe>
</div>
以下是上面的 iframe 调用的按钮的 HTMl 代码:
<button type="button" id="SuperWebF1" title="Continue" class="button">Continue</button>
这是我在页面加载时用来触发按钮单击的 jQuery 函数:
$('#SuperWebF1').trigger( "click" );
但只有当我将 jQuery 代码放在按钮源中并且 iframe 使用脚本调用按钮时,这种情况才有效。
我想创建一个从 iframe 外部单击按钮的事件。
是否可以 ?
提前致谢!
注意:如果 iframe 的 src 指向跨域页面,.contents() 不起作用。更多的:iframe跨域问题 https://stackoverflow.com/questions/9393532/cross-domain-iframe-issue and 获取跨域iframe的DOM内容 https://stackoverflow.com/questions/6170925/get-dom-content-of-cross-domain-iframe
//execute code after DOM is ready
$(function() {
//find iframe
let iframe = $('#main_iframe');
//find button inside iframe
let button = iframe.contents().find('#main_button');
//trigger button click
button.trigger("click");
});
<!--Add jQuery library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
如果需要操作 iframe 内的另一个页面,请搜索官方 API 或 GET 参数。
例子:YouTube 嵌入视频 https://developers.google.com/youtube/player_parameters, Google 地图嵌入 Api https://developers.google.com/maps/documentation/embed/start
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)