简体   繁体   English

如何使用 jquery 处理第三部分 iframe 的点击事件?

[英]How to handle click event to 3rd part iframe using jquery?

I am using WordPress and WooCommerce.我正在使用 WordPress 和 WooCommerce。 When a product is added to cart, an iframe from 3rd party plugin hosted on a different url is loaded or inserted to the DOM dynamically.将产品添加到购物车时,来自托管在不同 url 上的 3rd 方插件的 iframe 会动态加载或插入到 DOM 中。

<li class="klarna-checkout-wrap">

  <iframe id="klarna-checkout-iframe" name="klarna-checkout-iframe" class="k-loading" scrolling="no" frameborder="0" style="height: 860px; width: 1px; min-width: 100%;" src="https://checkout-eu.playground.klarna.com/12345/checkout.html#%7B%22ORDER_URL%22%></iframe>

</li>

There is an element inside the iframe with an <div id="buy-button-next"></div> iframe 中有一个带有<div id="buy-button-next"></div>的元素

Do you know how can I access the element inside the iframe that has an id of "buy-button-next" ?您知道如何访问 iframe 中 ID 为"buy-button-next"的元素吗?

I tried the code below but it's not working..我尝试了下面的代码,但它不起作用..

  $('.klarna-checkout-wrap #klarna-checkout-iframe #buy-button-next').on('click', function(e) {
    alert('test123');
  });

Any idea is appreicated.任何想法都值得赞赏。 Thanks.谢谢。

You need to add your script on the page which is inside the Iframe.您需要在 Iframe 内的页面上添加脚本。 As it seems below is the source URL如下所示是源 URL

https://checkout-eu.playground.klarna.com/12345/checkout.html#%7B%22ORDER_URL%22% https://checkout-eu.playground.klarna.com/12345/checkout.html#%7B%22ORDER_URL%22%

goto that page and simply below code will work.转到那个页面,下面的代码就可以工作了。

$('#buy-button-next').on('click', function(e) {
    alert('test123');
  });

If you do not have access to the source URL then you can't do it.如果您无权访问源 URL,则无法访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM