简体   繁体   English

通过jQuery与同一域上的iframe进行交互

[英]Interact with iframe on same domain via jQuery

I want to interact with an iFrame that I am loading from the same domain. 我想与从同一个域加载的iFrame进行交互。

Shopify provides what is called an AppProxy and this allows folks to serve content from the same domain via this AppProxy . Shopify提供了所谓的AppProxy ,这使人们可以通过此AppProxy提供来自同一域的内容。 That's what I'm doing. 那就是我在做的

I have a shop https://test-4658.myshopify.com that when a user signs in and views their account an iframe pops up. 我有一家商店https://test-4658.myshopify.com ,当用户登录并查看其帐户时,会弹出iframe。 The shop is test and you can create an account to see if you'd like. 该商店已经过测试,您可以创建一个帐户来查看是否要购买。

The jQuery and HTML is below: jQuery和HTML如下:

var frame = "<div id='fresh-credit' style='position: fixed; overflow: auto; top: 0; right: 0; bottom: 0; left: 0; padding: 0; z-index: 2147483647; box-sizing: border-box;'>\n" + 
      "<div style='position: fixed; background-color: rgba(0,0,0,.4); top: 0; right: 0; bottom: 0; left: 0; box-sizing: border-box;'></div>\n" +
        "<div style='width: 400px; height: 470px; padding: 0px; background: transparent; margin: auto; max-width: 100%; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); box-sizing: border-box;'>\n" +
          "<iframe onload=this.style.visibility='visible' allowtransparency='true' style='visibility: visible; width: 100%; height: 100%; border: 0px; background: transparent;' src='https://test-4658.myshopify.com/apps/proxy/credit'></iframe>\n" +
        "</div>\n" +
    "</div>";

function showModal(callback){
  $(document).ready(function(){  
  if (document.location.pathname === '/account'){
    console.log("now");
  $('body').append($.parseHTML(frame));
  }
});
callback();
}

That part works great, modal shows perfect. 该部分效果很好,模态显示完美。 In the callback I have this function, with some alerts for testing: 在回调中,我具有此功能,并提供一些警报以进行测试:

function getMe(){

$(function(){
  $("iframe[src='https://test-4658.myshopify.com/apps/proxy/credit']").contents().find("#fresh-credit-button").click(function(){alert("yep it worked");});
  var button = $("iframe[src='https://test-4658.myshopify.com/apps/proxy/credit']").contents().find("#fresh-credit-button");
  console.log("Did I find the button? " + button );
  button.click(function(){
    console.log("clicked");
  });
});
}

showModal(getMe);

In the above code.. The console show's that I found the button in the iframe, but I can't click on it. 在上面的代码中。控制台显示的是我在iframe中找到了按钮,但是无法单击它。 Even though its from the same domain as the shop. 即使它与商店来自同一域。 AND if I put this code into the console, it works!! 而且,如果我将此代码放入控制台,它就可以工作!!

$("iframe[src='https://test-4658.myshopify.com/apps/proxy/credit']").contents().find("#fresh-credit-button").click(function(){alert("yep it worked");});

There are other apps on the store that are working just like this. 商店中还有其他类似的应用程序。 If you click on those black tabs at the bottom of the page you'll see them. 如果单击页面底部的黑色标签,则会看到它们。

What am I doing wrong? 我究竟做错了什么?

尝试此操作,单击是一种简写功能,但这可能会起作用。

$("iframe[src='https://test-4658.myshopify.com/apps/proxy/credit']").contents().find("#fresh-credit-button").on( "click", function(){alert("yep it worked");});

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

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