【问题标题】:Trying to click a button inside an Iframe with jQuery尝试使用 jQuery 单击 Iframe 内的按钮
【发布时间】:2014-12-06 16:33:17
【问题描述】:

伙计们,我正在尝试使用 jQuery 单击 iframe 内的按钮。

这是我创建的函数:

<script type="text/javascript">
     function ClickTheButton()
     {
        $('#InnerIframe').contents().find('#SuperWebF1').trigger( "click" );
     }
</script>

这是按钮被 iframe 调用时的 HTML 代码:

<button type="button"  id="SuperWebF1" title="Continue" class="button">Continue</button>

这是 iframe 的 HTML 代码:

<div id="OutDiv" class="outerdiv">
    <iframe src="http://beta.sportsdirect.bg/checkout/onepage/" id="InnerIframe" class="FrameCSS" scrolling="no"></iframe>
</div>

当我在 Chrome 的控制台中运行 ClickTheButton() 时,我收到了这个错误: TypeError: Cannot read property 'contents' of null

当我执行这个功能时,按钮没有被点击,但它必须是......

各位,我的错在哪里? 如何点击 iframe 内的按钮?

提前致谢!

【问题讨论】:

    标签: javascript jquery html iframe


    【解决方案1】:

    您无法访问从与您不同的服务器加载的 iframe 的内容。这是出于安全原因。

    有关详细信息,请参阅例如https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policyhttp://javascript.info/tutorial/same-origin-security-policy

    【讨论】:

    • 内容和 iframe 都在我的服务器上。加载 iframe 的页面和 iframe 的内容都在我的服务器上!!!
    • 他们在不同的域上吗?如果您控制两个页面,您应该能够通过在它们中设置 document.domain 来使其工作,如我链接的页面中所述。
    猜你喜欢
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    相关资源
    最近更新 更多