获取文本javascript+iframe
get text javascript + iframe
我有html代码
站点index.html
<html>
<body>
<iframe id="html2" frameborder="0" src="html2.htm" marginwidth="0" marginheight="0" style="height: 492px;"></iframe>
</body>
</html>
网站html2.htm
<html>
<body>
<div><p id='123'>hello</p></div>
</body>
</html>
当我在站点index.html中使用document.getElementById('123')
时return = null;
如何获得
您需要首先通过以下方式访问iframe的innerDoc:
var iframe = document.getElementById('html2');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
一旦你得到对象,现在你可以访问:
innerDoc.getElementById('divId');
对于html中的ID,您需要遵循以下规则:
ID应匹配:
[A-Za-z][-A-Za-z0-9_:.]*
- 必须以A-Z或A-Z字符开头
- 可能包含_(连字符)、:(colan)和。(点)
但是应该避免CCD_ 3和CCD_。
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 使用php或javascript从facebook相册URL中删除多余的部分
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在JavaScript中将字符串转换为函数引用
- 模糊事件的Javascript测试
- Javascript更改图标
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 如何使用WCF服务和javascript表单post上传.doc文件
- javascript结合了数组和字典
- 这是什么 ==- javascript 运算符
- 从javascript创建一个列表
- 无法在通过jQuery的ajax加载的页面中执行javascript
- Javascript:selenium Web驱动程序isDisplayed()不工作
- 如何通过ajax刷新JSF填充的javascript变量
- 如何在Javascript中将JSon对象转换为数组
- Javascript生成的表单未提交
- 使用javascript将动态表从一个html页面打印到另一个html页
- 通过javascript重定向html传递php变量