這篇文章主要介紹了jQuery中attr()方法用法,實(shí)例分析了attr()方法的功能、定義及設(shè)置或返回匹配元素屬性值的使用技巧,需要的朋友可以參考下
本文實(shí)例講述了jQuery中attr()方法用法。分享給大家供大家參考。具體分析如下:
此方法設(shè)置或返回匹配元素的屬性值。
attr()方法根據(jù)參數(shù)的不同,功能也不同。
語(yǔ)法結(jié)構(gòu)一:
獲取第一個(gè)匹配元素指定屬性的屬性值。
復(fù)制代碼代碼如下:
$(selector).attr(name)
參數(shù)列表:
參數(shù) | 描述 |
name | 定義要獲取其值的屬性名稱(chēng)。 |
實(shí)例代碼:
復(fù)制代碼代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.survivalescaperooms.com/" />
<title>attr()函數(shù)-武林網(wǎng)</title>
<style type="text/css">
.font{
font-size:18px;
color:yellow
}
.bg{
background:#336;
}
.second{
color:green
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
alert($("div").attr("class"));
})
})
</script>
</head>
<body>
<div class="font bg">我是第一個(gè)div</div>
<div class="second">我是第二個(gè)div</div>
<button id="btn">點(diǎn)擊查看效果</button>
</body>
</html>
以上代碼中,點(diǎn)擊按鈕可以彈出匹配元素集合中,第一個(gè)元素的class屬性值。
語(yǔ)法結(jié)構(gòu)二:
為匹配元素指定的屬性設(shè)置屬性值。
復(fù)制代碼代碼如下:
$(selector).attr(attribute,value)
參數(shù)列表:
參數(shù) | 描述 |
attribute | 定義要設(shè)置值的屬性名稱(chēng)。 |
value | 定義要設(shè)置的屬性值。 |
實(shí)例代碼:
復(fù)制代碼代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.survivalescaperooms.com/" />
<title>attr()函數(shù)-武林網(wǎng)</title>
<style type="text/css">
div{
width:200px;
height:200px;
border:1px solid blue
}
.font{
font-size:18px;
color:yellow
}
.bg{
background:#336;
}
.reset{
color:green;
font-size:20px;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
$("div").attr("class","reset");
});
})
</script>
</head>
<body>
<div class="font bg">武林網(wǎng)歡迎您</div>
<button id="btn">點(diǎn)擊查看效果</button>
</body>
</html>
以上代碼中, 當(dāng)點(diǎn)擊按鈕的時(shí)候,能夠重新設(shè)置div元素的class屬性值。
語(yǔ)法結(jié)構(gòu)三:
將“名/值”形式的對(duì)象設(shè)置為匹配元素的屬性。可以一次性設(shè)置多組“名/值”對(duì),對(duì)匹配元素屬性進(jìn)行設(shè)置。
復(fù)制代碼代碼如下:
$(selector).attr(properties)
參數(shù)列表:
參數(shù) | 描述 |
attribute:value | 定義屬性名/值對(duì) |
實(shí)例代碼:
復(fù)制代碼代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.survivalescaperooms.com/" />
<title>attr()函數(shù)-武林網(wǎng)</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
$("td").attr({width:"200",height:"300"});
})
})
</script>
</head>
<body>
<table border="1">
<tr>
<td>歡迎來(lái)到武林網(wǎng)</td>
</tr>
</table>
<button id="btn">點(diǎn)擊查看效果</button>
</body>
</html>
以上代碼中,可以設(shè)置單元格的寬度和高度。
語(yǔ)法結(jié)構(gòu)四:通過(guò)函數(shù)返回值設(shè)置屬性值。
復(fù)制代碼代碼如下:
$(selector).attr(name,function(index,oldvalue))
參數(shù)列表:
參數(shù) | 描述 |
name | 定義要設(shè)置值的屬性的名稱(chēng)。 |
function(index,oldvalue) | 定義返回屬性值的函數(shù) index - 可選,接受選擇器的索引位置。 class - 可選,接受選擇器的當(dāng)前的屬性值。 |
實(shí)例代碼:
復(fù)制代碼代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.survivalescaperooms.com/" />
<title>attr()函數(shù)-武林網(wǎng)</title>
<style type="text/css">
div{
height:200px;
width:200px;
border:1px solid blue
}
.font{
font-size:18px;
}
.bg{
background:#336;
color:red;
}
.reset{
font-size:20px;
color:green;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
$("div").attr("class" ,function(){
return "reset"
})
})
})
</script>
</head>
<body>
<div class="font bg">武林網(wǎng)歡迎您</div>
<button id="btn">點(diǎn)擊查看效果</button>
</body>
</html>
以上代碼中,同樣可以設(shè)置div的class屬性值,只是屬性值是以函數(shù)的返回值方式獲得的。
希望本文所述對(duì)大家的jQuery程序設(shè)計(jì)有所幫助。