JavaScript:从iframe内爬回dom树

JavaScript: Climb back up dom tree from within iframe

本文关键字:dom iframe JavaScript      更新时间:2023-09-26

我有以下代码:

<div id="hello">
    <iframe stc="foo/bar.html"></iframe>
</div>

iframe的内容:

<!DOCTYPE html>
<html>
<body>
    <div id="foo"></div>
</body>
</html>

如何获得id为"hello"的div ?

如果所有这些都在同一页上,我会这样做:

document.getElementById('foo').parentNode;

注意:我不能在这个项目中使用jQuery。我也不能按id选择因为我不知道父结点的id是什么。如果它一直是foo,那么我可以这样做:

parent.document.getElementById('foo')

可以按类名选择元素吗?如果是这样,可以这样做:http://www.anyexample.com/webdev/javascript/javascript_getelementsbyclass_function.xml