有谁知道,为什么 jquery 的行为是这样的:
var $test = $(window).add(document).add("body");
$test.is(window) -> false
$test.is(document) -> true
$test.is("body") -> true
如何通过“is”判断一个 jquery 对象是否包含窗口?
最佳答案
var $test = $(window).add(document).add("body");
$test.is(function(index, elements) {
return this === window;
});
jQuery“is()”方法检查传递的元素的节点类型。 Document 的 nodeType 为 9。Body 的 nodeType 为 1。Window 没有 nodeType,因此“undefined”的计算结果为 false。要解决这个问题,请构建您自己的过滤功能,如上图所示。
关于javascript - 检查 jquery 对象内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32498273/