Learn JQuery's html () & amp; val () & amp; CSS_DOM operations step by step

Source: Internet
Author: User

Learn JQuery's html () & val () & CSS_DOM operations step by step

Set and obtain HTML, text, and value

Read and set the HTML content of an element: html (). This method can be used for XHTML, but not for XML documents.
Read and set text content in an element: text (). This method can be used for both XHTML and XML documents.
Read and set the value in an element: val () --- this method is similar to the value attribute in JavaScript. for the text box, drop-down list box, single comment this method can return the value of the element (multiple select box can only return the first value ). if you select multiple drop-down lists, an array containing all selected values is returned.

<! Doctype html public "-// W3C // dtd html 4.01 // EN" "http://www.w3.org/TR/html4/strict.dtd"> 
Use the val () method



<! Doctype html public "-// W3C // dtd html 4.01 // EN" "http://www.w3.org/TR/html4/strict.dtd"> 

Common node traversal methods

Obtain a set of all child elements that match an element: children (). This method considers only child elements, not any child elements.
Obtains the set of peer elements next to the Matching Element (but only one element in the Set): next ()
Obtains the set of peer elements adjacent to the Matching Element (but only one element in the Set): prev ()
Obtain all peer elements before and after matching elements: siblings ()

Style operations

Obtain the class and set the class: class is an attribute of the element. Therefore, you can use the attr () method to obtain the class and set the class.
Append style: addClass ()
Remove style: removeClass () --- delete all or the specified class from the matched element
Switching style: toggleClass () --- controls repeated switching of a style. If the class name exists, delete it. If the class name does not exist, add it.
Determines whether a style exists: hasClass () --- determines whether the element contains a class. If yes, true is returned; otherwise, false is returned.

CSS-DOM operation

Get and set the style attribute of the element: css ()
Get and set element transparency: opacity attribute
Obtain and set the element height, width: height (), width (). when setting the value, if only a number is passed, the default unit is px. if you want to use another unit, you need to pass a string, for example, $ ("p: first "). height ("2em ");
Obtains the relative displacement of an element in the current window: offset (). The returned object contains two attributes: top and left. This method is only valid for visible elements.

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 


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.