我需要根除嵌套在 iframe 中的 iframe。
我的代码的结构如下所示。它在所有浏览器中工作正常,但在 ie 中会导致无限循环。然而,当它处于无限循环时,CPU根本不会被提升。
var getNested_iframes = function (document_element) {
$.each(document_element.find("iframe"), function () {
getNested_iframes($($(this)[0].document));
});
alert(".");
}
getNested_iframes($(myWindow._element));
alert("done");
最佳答案
我不太清楚为什么你要这样做,但“$($(this)[0].document)”对我来说看起来有点可疑。在任何情况下,值得注意的是 jquery 对象本质上是任意大小的列表,因此您可以通过迭代而不是递归来完成此操作(并且从递归交换到迭代,当您可以做到这一点时,通常会导致像这样的问题来简化或收拾)。尝试以下方法
$startlist = $(myWindow._element);
while($startlist.length > 0)
{
$nextlist = $startlist.find("iframe");
alert("(" + nextlist.length + ")");
$startlist = $nextlist;
}
alert("done");
它甚至可以让您跟踪每个级别有多少个、有多少个级别等等。
关于jquery - 奇怪的无限循环 - 仅在 ie 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9024617/