从父标记获取id的元素

时间:2016-12-15 05:51:23

标签: javascript jquery html iframe

我的DOM如下所示。

<html><head>...</head><body><script>...</script>
<iframe id="frame">
    #document
    <html>...</html> <!-- 2 -->
</iframe>
</body>
</html>

我在iframe中约束部分(即html 2)。在html_2部分中,我使用document.ready致电id以获取iframe getElementById,但我得到null。如何获取iframe的ID和其他属性值?

3 个答案:

答案 0 :(得分:1)

我这样做了,如下,

$("#iframe", parent.document).attr("id");
顺便说一句,感谢所有通过重要评论回答并帮助我的人。

答案 1 :(得分:0)

&#13;
&#13;
$("iframe").each(function() {
  alert($(this).attr("id"));

})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>...</head>

<body>
  <script>
    ...
  </script>
  <iframe id="frame">

  </iframe>
  <iframe id="frame1">

  </iframe>
  <iframe id="frame2">

  </iframe>
</body>

</html>
&#13;
&#13;
&#13;

  1. 使用.attr("id")
  2. 对于多个iframe使用.each()迭代iver所有iframe
  3. 使用此功能获取当前iframe

答案 2 :(得分:0)

尝试使用:

document.getElementById("frame").contentWindow.document.getElementById("div1")
相关问题