JavaScript reinforcement tutorial-jQuery-get content and attributes

Source: Internet
Author: User
JQuery has a powerful way to operate HTML elements and attributes. A very important part of jQueryDOM operations on jQuery is the ability to operate on DOM. This article is an official HTML5 training course for H5EDU institutions. It mainly introduces: JavaScript reinforcement course-jQuery-get content and attributes.

JQuery has a powerful way to operate HTML elements and attributes.

JQuery DOM operations

A very important part of jQuery is its ability to operate the DOM.

JQuery provides a series of DOM-related methods, which makes it easy to access and manipulate elements and attributes.

Tip: DOM = Document Object Model (Document Object Model)

DOM defines the criteria for accessing HTML and XML documents:

"The W3C Document Object Model is independent of the platform and language interface, allowing programs and scripts to dynamically access and update document content, structures, and styles ."

Get content-text (), html (), and val ()

Three simple and practical jQuery methods for DOM operations:

Text ()-set or return the text content of the selected Element
Html ()-set or return the content of the selected element (including HTML tags)
Val ()-set or return the value of a form field

The following example shows how to obtain content using jQuery text () and html () methods:

Instance

$("#btn1").click(function(){ alert("Text: " + $("#test").text()); }); $("#btn2").click(function(){ alert("HTML: " + $("#test").html()); });


The following example shows how to obtain the value of an input field through the jQuery val () method:

Instance

$("#btn1").click(function(){ alert("Value: " + $("#test").val()); });


Get attributes-attr ()

The jQuery attr () method is used to obtain the attribute value.

The following example shows how to obtain the value of the href attribute in the link:

Instance

$("button").click(function(){ alert($("#w3s").attr("href")); });

The above is the JavaScript reinforcement tutorial-jQuery-the content for obtaining content and attributes. For more information, see PHP Chinese website (www.php1.cn )!

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.