我需要编写一个脚本,它将使用document.write来生成一些输出,但是我需要知道它包含哪个元素,例如:
<p>
paragraph 1
<script src="test.js" type="text/javascript"></script>
</p>
Run Code Online (Sandbox Code Playgroud)
我需要获得对p标签的引用...
谢谢!
use*_*621 11
在运行脚本时,文档仅部分加载.所以你的脚本元素将是文档中的最后一个节点:
var target = document.documentElement; // start at the root element
while (target.childNodes.length && target.lastChild.nodeType == 1) { // find last HTMLElement child node
target = target.lastChild;
}
// target is now the script element
alert(target.parentNode); // this is p
Run Code Online (Sandbox Code Playgroud)
示例: http ://jsfiddle.net/NqN3S/
<p>
paragraph 1
<script id="test" type="text/javascript">
var scpt = document.getElementById( "test" );
var p = scpt.parentNode;
p.removeChild( scpt );
alert( "old content: " + p.innerHTML );
p.innerHTML = "some new content";
</script>
</p>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7349 次 |
| 最近记录: |