jquery Selector-base selector, hierarchy Selector

Source: Internet
Author: User

Note:$ ("#tt") gets the object forever, without this element on the instant page. Therefore, when you want to use jquery to check whether an element exists on a Web page, you cannot use the following code :

The IF ($ ("#tt")) {} should be judged based on the length of the element obtained, the code is as follows: if ($ ("#tt"). length>0) {} or Convert DOM object to determine the code as follows: if ($ ("tt") [0]) {}


Basic Selector

#id ID Selector $ ("#test") select an element with ID test

. class Selector $ (". Test") to select an element of class test

Element element Selector $ ("P") selects all <p> elements

* Match all elements $ ("*") Select all elements

Selector1,slector2,...., Selectorn Match to each element after merging returns $ ("Div,span,p.myclass") Select All <div>,<span> And a set of elements with a <p> tag of class MyClass

eg

$ ("#one"). CSS ("Background", "#000000"),//jquerydocument.getelementbyid ("one"). Style.color = "Red";//dom


Hierarchy Selector

$ ("div span") Select all the span elements in the DIV

$ ("Div>span") Select the span sub-element under Div

$ (". One+div") Select the next div sibling element of class one

$ ("#two ~div") selects all div sibling elements that follow the element with ID of.

Note: The following two selectors can be replaced in a simpler way

$ (". One"). Next ("div");

$ ("#two"). Nextall ("div");

The top two methods are to select the behind DIV element

$ ("#two"). Siblings ("div"); get all of the sibling div before and after












jquery Selector-base selector, hierarchy Selector

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.