国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

jQuery - 獲取內(nèi)容和屬性

jQuery DOM 操作

jQuery 中非常重要的部分,就是操作 DOM 的能力。

jQuery 提供一系列與 DOM 相關(guān)的方法,這使訪問和操作元素和屬性變得很容易。


三個簡單實用的用于 DOM 操作的 jQuery 方法:

text() - 設(shè)置或返回所選元素的文本內(nèi)容

html() - 設(shè)置或返回所選元素的內(nèi)容(包括 HTML 標(biāo)記)

val() - 設(shè)置或返回表單字段的值

演示如何通過 jQuery text() 和 html() 方法來獲得內(nèi)容:

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

演示如何通過 jQuery val() 方法獲得輸入字段的值:

<!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中文網(wǎng)"></p>
<button>顯示值</button>
</body>
</html>

attr()屬性

jQuery attr() 方法用于獲取屬性值。

演示如何獲得鏈接中 href 屬性的值:

<!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($("#php").attr("href"));
  });
});
</script>
</head>
<body>
<p><a href="http://miracleart.cn" id="php">php中文網(wǎng)</a></p>
<button>顯示 href 屬性的值</button>
</body>
</html>


繼續(xù)學(xué)習(xí)
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#btn").click(function(){ $("div").attr("class","reset"); }); }) </script> <style type="text/css"> div{ width:200px; height:200px; border:1px solid blue; } .font{ font-size:18px; color:yellow; } .bg{ background:pink; } .reset{ color:green; font-size:20px; } </style> </head> <body> <div class="font bg">php.cn歡迎您</div> <button id="btn">點擊查看效果</button> </body> </html>
提交重置代碼