jQuery 庫包含了很多用來改變和操作 HTML 元素及其屬性的方法。
其中一個非常重要的部分就是 jQuery 可以用來操作 DOM。
本篇介紹使用 jQuery 來取得 DOM 節(jié)點元素的值或?qū)傩浴?/p>
其中三個簡單而有用的方法如下:
例如,下面代碼使用 html()和 text()方法取得 HTML 元素的內(nèi)容:
$("#btn1").click(function(){
alert("Text: " + $("#test").text());
});
$("#btn2").click(function(){
alert("HTML: " + $("#test").html());
});
下面的代碼取得 Form 中 Input 的內(nèi)容:
$("#btn1").click(function(){
alert("Value: " + $("#test").val());
});
除了上面的方法外,attr()方法用來取得某個元素的屬性:
下面代碼用來取得鏈接的 href 屬性:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JQuery Demo</title>
<script src="scripts/jquery-1.9.1.js"></script>
</script>
<script>
$(document).ready(function () {
$("button").click(function () {
alert($("#guidebee").attr("href"));
});
});
</script>
</head>
<body>
<p><a
id="guidebee">
imobilebbs.com
</a></p>
<button>Show href Value</button>
</body>
</html>
http://wiki.jikexueyuan.com/project/jquery-tutorial/images/4.png" alt="" />