获取iframe里边的元素
时间: 2023-08-17 16:09:44 浏览: 51
可以使用以下代码来获取iframe中的元素:
```
var iframe = document.getElementById('myiframe');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var element = innerDoc.getElementById('myelement');
```
其中,'myiframe'是iframe的id,'myelement'是要获取的元素的id。
相关问题
vue获取iframe中元素
要获取iframe中的元素,可以通过以下步骤:
1. 首先获取iframe元素,可以使用document.getElementById()或者document.querySelector()等方法。
2. 然后通过iframe的contentWindow属性获取到iframe中的window对象。
3. 使用window对象的document属性获取到iframe中的document对象。
4. 最后就可以使用document对象的各种方法获取到iframe中的元素了。
以下是一个示例代码:
```javascript
var iframe = document.getElementById("myiframe");
var iframeWindow = iframe.contentWindow;
var iframeDocument = iframeWindow.document;
var iframeElement = iframeDocument.getElementById("myelement");
```
需要注意的是,获取iframe中的元素可能会受到跨域限制,需要确保iframe中的源与父页面的源相同。
js 获取iframe dom元素
可以使用以下代码获取iframe中的DOM元素:
```javascript
var iframe = document.getElementById("myIframe");
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
var domElement = iframeDoc.getElementById("myElement");
```
其中,"myIframe"是iframe的id,"myElement"是需要获取的DOM元素的id。