0

Jquery:如何选择特定 iframe 内的所有 iframe?我有一个特定的 iframe,id="main_frame" 到目前为止我尝试了这个,但它不起作用:

$("#main_frame").contents().find("iframe").each(function() { 
  data = $(this);
  //somecode
});

这仅获取#main_frame 的数据。

4

1 回答 1

1

给定这样的 HTML:

<body>
    <div>Outer Stuff</div>
    <iframe id="main_frame">            
        <iframe id="inner1">
            <div>
                <span>Some content</span>
            </div>
            <iframe>
                <div>Inner X2</div>
                </iframe>
            </iframe>
            <iframe id="inner2">
            <div>
                <span>Some more stuff in iframe #2</span>
            </div>
        </iframe>
    </iframe>
</body>

此脚本将选择除最外层之外的所有 iframe 元素:

<script type="text/javascript">

    $(function() {      
        $(document).find('iframe').each(function() {
            console.log($(this).html());
        });
    });     

</script>
于 2013-03-28T13:05:53.923 回答