您可以从同一站点定位 iFrame 中的元素吗?
Can you target an element in an iFrame from the same site?
我想做的是在页面上的 iframe 中打开迷你购物车 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": "€ 30,00"}
</pre>
</body>
</html>
</iframe>
对于任何有问题的人,添加 = sandbox="allow-same-origin allow-scripts" 对我有用
我想做的是在页面上的 iframe 中打开迷你购物车 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": "€ 30,00"}
</pre>
</body>
</html>
</iframe>
对于任何有问题的人,添加 = sandbox="allow-same-origin allow-scripts" 对我有用