gpt4 book ai didi

jquery - *递归地*在嵌套 iframe 中查找给定元素

转载 作者:行者123 更新时间:2023-12-01 02:45:48 25 4
gpt4 key购买 nike

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

谢谢

最佳答案

前提是框架访问不受同源策略限制:

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');

关于jquery - *递归地*在嵌套 iframe 中查找给定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11224324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com