首页 > 解决方案 > 您可以从同一站点定位 iFrame 中的元素吗?

问题描述

我要做的是在页面上的 iframe 中打开 minicart url,这样我就可以从购物车中获取购物篮总数。我已经能够做到这一点,但是当我尝试定位 iframe 上的内容时,我无法做到这一点。在站点上,当我使用 console.log “pre”时,我确实看到在 html 集合中,innerHTML 确实显示了输出的内容。知道如何或是否能够针对此目标吗?

var iframevar = document.getElementById('freeGiftiFrame');
var elmnt = iframevar.contentWindow.document.getElementsByTagName("pre");
console.log(elmnt)
<iframe id="freeGiftiFrame" class="cartPopout" name="iframe_a" height="100px" width="100%">
  <html>
  <head></head>
    <body>
      <pre style="word-wrap: break-word; white-space: pre-wrap;">
      {"miniCartCount": 1, "miniCartPrice": "€&amp;nbsp;30,00"}
      </pre>
    </body>
  </html>
</iframe>

标签: javascriptiframe

解决方案


对于任何有问题的人,添加 = sandbox="allow-same-origin allow-scripts" 对我有用


推荐阅读