jQuery capture
jQuery - Get content and attributes
jQuery has powerful methods for manipulating HTML elements and attributes.
jQuery DOM manipulation
A very important part of jQuery is the ability to manipulate the DOM.
jQuery provides a set of DOM-related methods that make it easy to access and manipulate elements and attributes.
##Note: DOM = Document Object Model
DOM defines the standard for accessing HTML and XML documents:
"The W3C Document Object Model is a platform- and language-independent interface that allows programs and scripts to dynamically access and update the content of a document. , structure and style. "
Get content - text(), html() and val().
Three simple and practical jQuery methods for DOM operations:
- text() - Set or return the selected element The text content of
- html() - Sets or returns the content of the selected element (including HTML tags)
- val() - Sets or Return the value of a form field
- The following example demonstrates how to obtain content through the jQuery text() and html() methods:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
alert("Text: " + $("#test").text());
});
$("#btn2").click(function(){
alert("HTML: " + $("#test").html());
});
});
</script>
</head>
<body>
<p id="test">这是段落中的 <b>粗体</b> 文本。</p>
<button id="btn1">显示文本</button>
<button id="btn2">显示 HTML</button>
</body>
</html>
Run Example»Click the "Run Example" button to view the online exampleThe following example demonstrates how to pass the jQuery val() method Get the value of the input field:
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("值为: " + $("#test").val());
});
});
</script>
</head>
<body>
<p>名称: <input type="text" id="test" value="php中文网"></p>
<button>显示值</button>
</body>
</html>
Run Instance»Click the "Run Instance" button to view the online instance
Get attributes - attr()jQuery attr() method is used to get attribute values.
The following example demonstrates how to get the value of the href attribute in the link:
Example<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert($("#runoob").attr("href"));
});
});
</script>
</head>
<body>
<p><a href="http://www.php.cn" id="runoob">php中文网</a></p>
<button>显示 href 属性的值</button>
</body>
</html>
Run Example»Click the "Run Instance" button to view the online instance