Home > Web Front-end > JS Tutorial > body text

JavaScript Enhancement Tutorial - jQuery - Get content and attributes

黄舟
Release: 2017-01-21 15:58:05
Original
1232 people have browsed it

This article is the official HTML5 training tutorial of the H5EDU organization. It mainly introduces: JavaScript enhancement tutorial - jQuery - Obtaining content and attributes

jQuery has a powerful method of manipulating HTML elements and attributes.

jQuery DOM operation

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

jQuery provides a set of DOM-related methods that make it easy to access and manipulate elements and attributes.

Tip: DOM = Document Object Model

DOM defines the standard for accessing HTML and XML documents:

"The W3C Document Object Model is platform and language independent An interface that allows programs and scripts to dynamically access and update the content, structure, and style of documents."

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

Three simple and practical methods. jQuery methods for DOM manipulation:

text() - Sets or returns the text content of the selected element
html() - Sets or returns the content (including HTML markup) of the selected element
val() - Sets or returns the value of a form field

The following example demonstrates how to obtain content through the jQuery text() and html() methods:

Example

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


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

Example

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


Get attributes - attr()

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

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

Example

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

The above is the JavaScript enhancement tutorial-jQuery - Obtaining content and attributes Content, for more related content, please pay attention to the PHP Chinese website (www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template