【发布时间】:2012-05-26 18:20:59
【问题描述】:
我已经搜索了这个问题的答案,但不知道该怎么做。
我想访问已包含在对象标记中的 div 的内容。
我的 include.htm 文件:
<div id="includedDiv">This is the included page</div>
我尝试过的:
<html>
<head>
<title>Get element from object included page</title>
</head>
<body>
<p>Test: This is the main page</p>
<object id="obj" data="include.htm"></object>
<script>
//alert(document.getElementById("includedDiv").firstChild.nodeValue);
//alert((document.getElementById("obj")).document.getElementById("includedDiv").firstChild.nodeValue);
alert(document.getElementById("obj")["includedDiv"]);
</script>
</body>
</html>
没有任何警报向我打印消息“这是包含的页面”,有没有办法从 DOM 获取此内容?
编辑: window[0].document.getElementById("includedDiv").firstChild.nodeValue;是通过 DOM 访问的好答案,对象标记只是创建另一个窗口 :) http://www.w3schools.com/jsref/prop_win_length.asp
【问题讨论】:
-
可能的线索,我要访问的内容在外部页面,有人说object标签立即关闭,所以没有子找到stackoverflow.com/questions/1981191/…似乎可以加载外部的内容使用 jQuery 加载方法或使用原生 Javascript AJAX 方法的 HTML 页面 XMLHttpRequest stackoverflow.com/questions/4728520/…
-
我不明白我们为什么要把事情复杂化,div 内容被加载,我们可以在页面上看到消息,所以它应该在某个地方,可以通过 DOM 访问!
-
@baptx window[0].document.getElementById("includedDiv").firstChild.nodeValue; :)
-
请注意,查看
<object>的能力可能受同源政策的约束。 -
这不是重点。你可能会发现它不适用于
... data="http://somewhere.else.com" ...。
标签: javascript html dom