在当前文档中查找给定的"div.myClass“元素以及它可能具有的任何iframe(以及它们的嵌套iframe)的最佳方法是什么?
谢谢
发布于 2012-06-27 19:06:42
假设帧访问不受同源策略的限制:
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');https://stackoverflow.com/questions/11224324
复制相似问题