jquery Learning Notes

Source: Internet
Author: User

One, jquery selector:

1. jquery element Selector

$ ("P.text")//Select all <p> elements of class= "text"
$ ("P#text")//Select all <p> elements of id= "text"

2. jquery attribute Selector

$ ("[href]")//Select all elements with an href attribute. $ ("[href= ' # ']")//Select all elements with an HREF value equal to "#". $ ("[href!= ' # ']")//Select all elements with an HREF value that is not equal to "#". $ ("[href$= '. jpg ']")//Select all the elements with an href value ending with ". jpg".

3. JQuery CSS Selector

$ ("P"). CSS ("Background-color", "Red");  Always forget to enclose red quotes!!!

Ii. Events:

$ (document). Ready (function () {
Some code
})

Ready means that the document structure has been loaded (not including non-text media files such as pictures), and is relative to onload, which indicates that all elements, including pictures and so on, are loaded.

$ (selector). Click (function () {//some code})

Click is a more common event that triggers or binds a function to the clicked event of the selected element. There are also Dbclick (double-click), MouseOver (hover), focus (get focus), and so on.

Iii. Obtaining content and attributes (text (), HTML (), Val (), attr ())

1. Text ()-Sets or returns the text content of the selected element

Similar to the InnerText property in JavaScript, it can be used to read or set the text content of an element.

<p><strong>hello World</strong></p>alert ($ ("P"). text ());  Hello World

2, HTML ()-Sets or returns the contents of the selected element (including HTML tags)

Similar to the innerHTML property in JavaScript, it can be used to read or set HTML content in an element.

<p><strong>hello World</strong></p>alert ($ ("P"). html ());  <strong>hello world</strong>

3, Val ()-Sets or returns the value of a form field

Similar to the Value property in JavaScript, it can be used to set or get the value of an element.

<p>name: <input type= "text" name= "user" value= "Hello World"/></p>alert ($ ("[type= ' text ']"). Val ());  Hello World

4, attr ()-for getting, setting property values

<p id= "P1" name= "Content" >good Night</p>alert ($ ("#p1"). attr ("name"));   Content

$ (selector). attr (attribute,value)//Set property value
$ ("img"). attr ("width", "180");

  

jquery Learning Notes

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.