This article is excerpted from "Sharp jQuery", with a little addition of my own.
The spaces in the selector cannot be ignored. One more space or one less space may lead to completely different results. Let’s look at an example.
First construct the following HTML code:
Jquery tutorial
Jquery learning
Jquery plug-in
PHP learning
Jquery plug-in tutorial
Jquery plug-in learning
jQuery code:
var $test_a = $(".test :hidden");//jQuery with spaces Selector
var $test_b = $(".test:hidden");//jQuery selector without spaces
var len_a = $test_a.length;
var len_b = $test_b.length;
alert("The jQuery elements selected by the jQuery selector with spaces are: " len_a "");//The output is 4
alert("The jQuery elements selected by the jQuery selector without spaces Is: "len_b "pieces");//The output is 3
The reason why different results appear is that the descendant selector and the filter selector are different
var $test_a = $(".test :hidden");//with spaces The jQuery selector
The above code selects the hidden elements inside the element with class "test". (Descendant selector)
var $test_b = $(" .test:hidden");//jQuery selector without spaces
The above code selects the hidden element with class "test"
Note: Some selectors must use spaces. If there are no spaces, the element cannot be obtained, for example:
$("select:selected").length;//No matter at any time, this selector cannot get the element, this length It must be 0
$("select :selected");//This is correct
Some selectors must be used without spaces. If there are spaces, then The element cannot be obtained, for example:
$("input: checked").length;//No matter when, this selector cannot get an element, the length must be 0
$("input:checked");//This is correct
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn