attr方法:獲取和設置屬性節點,attr方法可以設置兩個參數或一個參數
獲取:設置一個參數是獲取屬性節點,注意點,無論找到多少元素,都只會獲取到第一個元素
設置:設置兩個參數是設置屬性節點,找到多少元素就設置多少元素,沒有找到元素,就會給元素添加屬性節點並且設置值
romove方法:刪除找到元素的節點,找到多少元素有,就刪除元素的屬性節點
代碼如下所示
<html>
<head>
<title></title>
</head>
<style>
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
$('span').attr('class') //獲取屬性節點值,但只會取第一個
console.log($('span').attr('class'))
$('span').attr('class','wjw') //設置屬性節點的值,獲取到的元素都會設置
$('span').attr('age','18') //設置屬性節點的值,沒有找到該屬性節點就添加
$('span').removeAttr('name') //刪除屬性節點的值
})
</script>
<body>
<span class="demo" name="study"></span>
<span class="demo" name="walk"></span>
</body>
</html>
瀏覽器控制台效果圖

