CSS selector usage analysis in jquery _jquery

Source: Internet
Author: User

This example describes the CSS selector usage in jquery. Share to everyone for your reference. Specifically as follows:

jquery uses a set of CSS selectors, a total of 5 kinds, namely tag Selector, ID selector, class selector, universal selector and group selector, which are described as follows:

1. Tag Selector

Used to select an existing tag element in an HTML page, also known as an element selector, in the format: $ ("element"), such as $ ("div"), with Getelementbytagname

2. ID Selector

Used to get an element with an id attribute, in the following format: $ ("id"), with getElementById

Copy Code code as follows:
<input id= "Users" type= "text" value= "abc"/>
$ ("#users"). Val ();

3. Class Selector

Used to get an element with the class attribute, in the format: $ ("class")

Copy Code code as follows:
<input id= "txt" type= "text" class= "T"/>

To add a border style to this text box, first use the class attribute to bind the text box

Copy Code code as follows:
$ (". T"). CSS ("Border", "2px solid Blue");

4. Universal Selector

The Universal selector (*) matches all elements and is used in conjunction with context Search, which is to find all tags in the HTML page, $ ("*")

5. Group Selector

Also called the multi-element selector, which selects the results of all the specified selector combinations, the syntax is as follows:

Copy Code code as follows:
$ ("Selector1, Seletor2, Selector3")

Such as:

Copy Code code as follows:
$ ("Div, span, p.styleclass"). CSS ("Border", "1px solid Red");

Comprehensive Example:

Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>css selector </title>
<script type= "Text/javascript" src= "Jquery-1.7.min.js" ></script>
<style type= "Text/css" >
. One {border:1px solid #03F; float:left; margin:1em; padding:1.5em; width:106px;}
. Two {background-color: #FC0;}
. Three {background-color: #FCF;}
</style>
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ ("*"). CSS ("Font-size", "12px");
$ ("div"). addclass ("one");
$ ("#div1"). AddClass ("two");
$ ("#div2, #div3"). AddClass ("three");
$ ("H3, P"). CSS ("Color", "red"). attr ("Align", "center");
$ ("#span1"). HTML ("There is a total of" + $ (". One") of the elements in this web page that applies a class at the same time. Length + "+", while the elements of one class and three class are all "+ $ (". One.three "). Length +" one ") ;
});
</script>
<body>
<p><span id= "Span1" ></span></p>
<div id= "Div1" &GT;DIV element: id attribute is div1.</div>
<div id= "Div2" &GT;DIV element: id attribute is div2.</div>
<div id= "Div3" &GT;DIV element: id attribute is div3.</div>
</body>

The effect diagram looks like this:

I hope this article will help you with your jquery programming.

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.