首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在嵌套的iframe中*递归地*查找给定的元素

问在嵌套的iframe中*递归地*查找给定的元素
EN

Stack Overflow用户
提问于 2012-06-27 18:37:55
回答 1查看 3.2K关注 0票数 2

在当前文档中查找给定的"div.myClass“元素以及它可能具有的任何iframe(以及它们的嵌套iframe)的最佳方法是什么?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-27 19:06:42

假设帧访问不受同源策略的限制:

代码语言:javascript
复制
function getElem(selector, $root, $collection) {
    if (!$root) $root = $(document);
    if (!$collection) $collection = $();
    // Select all elements matching the selector under the root
    $collection = $collection.add($root.find(selector));
    // Loop through all frames
    $root.find('iframe,frame').each(function() {
        // Recursively call the function, setting "$root" to the frame's document
        getElem(selector, $(this).contents(), $collection);
    });
    return $collection;
}
// Example:
var $allImageElements = getElem('img');
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11224324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档