Examples and notes for using the jQuery selector with spaces _ jquery

Source: Internet
Author: User
The space in the selector cannot be ignored. A space or a space may have different results. The following example shows the differences in detail, for more information about the amount of friends you are interested in, refer to this article from "sharp jQuery" and add your own stuff.

The space in the selector cannot be ignored. A space or a space may have different results. The following is an example.

First, construct the following HTML code:

The Code is as follows:



Jquery tutorial


Jquery Learning


Jquery plugin


PHP Learning



Jquery plugin tutorial


Jquery plugin Learning



JQuery code:

The Code is as follows:


Var $ test_a = $ (". test: hidden"); // jQuery selector with spaces
Var $ test_ B = $ (". test: hidden"); // jQuery selector without spaces
Var len_a = $ test_a.length;
Var len_ B = $ test_ B .length;
Alert ("The jQuery element selected by the jQuery selector with spaces is:" + len_a + ""); // The output is 4
Alert ("The jQuery element selected by the jQuery selector without spaces is:" + len_ B + ""); // The output is 3


Different results appear, which is the difference between the descendant selector and the filter selector.

The Code is as follows:


Var $ test_a = $ (". test: hidden"); // jQuery selector with spaces


The code above selects the hidden element in the element whose class is "test. (Descendant selector)

The Code is as follows:


Var $ test_ B = $ (". test: hidden"); // jQuery selector without spaces


The above code selects the hidden class as the "test" element.

Note:
Some selector usage must be a space. If there is no space, no element can be obtained. For example:

The Code is as follows:


$ ("Select: selected"). length; // at any time, this selector cannot obtain the element, and the length must be 0.
$ ("Select: selected"); // This is correct.


Some selectors must be used without spaces. If spaces exist, no elements can be obtained. For example:

The Code is as follows:


$ ("Input: checked"). length; // at any time, this selector cannot obtain the element, and the length must be 0.
$ ("Input: checked"); // This is correct.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.