Validating jquery object contents

Does anyone know why jquery behaves like this:

var $test = $(window).add(document).add("body");
$test.is(window) -> false
$test.is(document) -> true
$test.is("body") -> true

How to find out if a jquery object contains a window through "is"?

+4
source share
1 answer
var $test = $(window).add(document).add("body");
$test.is(function(index, elements) {
    return this === window;
});

jQuery's "is ()" method checks the nodeType type of the elements to pass. The document has a nodeType of 9. The body is of type nodeType 1. The window does not have nodeType, ergo "undefined", which evaluates to false. To get around this, create your own filter function, as shown above.

+1
source

Source: https://habr.com/ru/post/1606742/


All Articles