js如何获取iframe里面的元素

要获取 iframe 中的元素,可以:直接访问 iframe 元素,通过 contentdocument 或 contentwindow 获取内部元素。使用 getelementbyid 访问具有特定 id 的元素。使用 queryselector 选择具有特定 css 选择器的元素。使用 queryselectorall 选择所有具有特定 css 选择器的元素。但请注意跨域限制,不能访问不同域的 iframe 中的元素。

(图1)

如何获取 iframe 中的元素

直接访问 iframe

最直接的方法是直接访问 iframe 元素并通过其 contentDocument 或 contentWindow 属性获取内部元素。

const iframe = document.querySelector('iframe');
const iframeDocument = iframe.contentDocument;
const iframeWindow = iframe.contentWindow;

const element = iframeDocument.getElementById('elementId');
1、本站目前拥有近 1000+ 精品收费资源,现在加入VIP会员即可全部下载。
2、本资源部分来源其他付费资源平台或互联网收集,如有侵权请联系及时处理。
SEA模板网 » js如何获取iframe里面的元素

发表评论

加入本站VIP会员订阅计划,海量资源免费查看

目前为止共有 3654 位优秀的VIP会员加入! 立刻加入VIP会员