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 )!