jQuery中prop()方法用法實例
本文實例講述了jQuery中prop()方法用法。分享給大家供大家參考。具體分析如下:
此方法可以獲取或者設(shè)置匹配元素的屬性值。
根據(jù)方法參數(shù)的不同,作用也有所不同。
語法結(jié)構(gòu)一:
當(dāng)參數(shù)為屬性名稱時,此方法能夠匹配元素集合中,第一個匹配元素指定屬性名稱的屬性值。
參數(shù)列表:
實例代碼:
實例一:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>prop()函數(shù)-腳本之家</title>
<style type="text/css">
ul{
list-style:none;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
alert($("input[type='checkbox']").prop("checked"));
})
</script>
</head>
<body>
<ul>
<li><input type="checkbox" checked="checked" value="1" /></li>
<li><input type="checkbox" value="2" /></li>
</ul>
</body>
</html>
以上代碼可以返回被選中的checkbox的屬性值。
實例代碼二:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>prop()函數(shù)-腳本之家</title>
<style type="text/css">
ul{
list-style:none;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
alert($("li").prop("id"));
})
</script>
</head>
<body>
<ul>
<li>腳本之家歡迎您</li>
<li id="mytest"><input type="checkbox" checked="checked" value="1" /></li>
<li id="second"><input type="checkbox" value="2" /></li>
</ul>
</body>
</html>
以上代碼中,由于li元素集合中第一個li元素并沒有id屬性,所以返回值為空。
語法結(jié)構(gòu)二:
以屬性的“名/值對”對象方式設(shè)置所有匹配元素的屬性值。
參數(shù)列表:
實例代碼:
實例一:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>prop()函數(shù)-腳本之家</title>
<style type="text/css">
ul{
list-style:none;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("input[type='checkbox']").prop({disabled:true})
})
</script>
</head>
<body>
<ul>
<li><input type="checkbox" value="1" /></li>
<li><input type="checkbox" value="2" /></li>
</ul>
</body>
</html>
以上代碼能夠?qū)⑦x中所有復(fù)選框。
實例二:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>prop()函數(shù)-腳本之家</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("td").prop({width:"200",height:"300"});
})
</script>
</head>
<body>
<table border="1">
<tr>
<td>歡迎來到腳本之家</td>
</tr>
</table>
</body>
</html>
以上代碼可以設(shè)置td的寬度和高度。
語法三:
以屬性名/值對方式設(shè)置所有匹配元素的屬性值。
參數(shù)列表:
實例代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>prop()函數(shù)-腳本之家</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").prop("class","reset");
})
})
</script>
</head>
<body>
<div class="font bg">腳本之家歡迎您</div>
<button id="btn">點擊查看效果</button>
</body>
</html>
以上代碼可以為div設(shè)置指定的樣式。
語法結(jié)構(gòu)四:
通過函數(shù)返回值設(shè)置屬性值。
參數(shù)列表:
實例代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>prop()函數(shù)-腳本之家</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").prop("class" ,function(){
return "reset"
})
})
})
</script>
</head>
<body>
<div class="font bg">腳本之家歡迎您</div>
<button id="btn">點擊查看效果</button>
</body>
</html>
以上代碼可以為div設(shè)置指定的樣式。
希望本文所述對大家的jQuery程序設(shè)計有所幫助。
- jquery獲取自定義屬性(attr和prop)實例介紹
- jquery中prop()方法和attr()方法的區(qū)別淺析
- js與jquery實時監(jiān)聽輸入框值的oninput與onpropertychange方法
- jQuery學(xué)習(xí)之prop和attr的區(qū)別示例介紹
- jquery下onpropertychange事件的綁定方法
- Jquery阻止事件冒泡 event.stopPropagation
- jquery 獲取自定義屬性(attr和prop)的實現(xiàn)代碼
- jQuery中attr()和prop()在修改checked屬性時的區(qū)別
- jQuery獲取attr()與prop()屬性值的方法及區(qū)別介紹
- 詳解jQuery中的prop()使用方法
相關(guān)文章
淺談jQuery hover(over, out)事件函數(shù)
下面小編就為大家?guī)硪黄獪\談jQuery hover(over, out)事件函數(shù)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,祝大家游戲愉快哦2016-12-12jQuery制作簡潔的多級聯(lián)動Select下拉框
省市多級聯(lián)動的select下拉框有很多種實現(xiàn)方式,度娘上隨便一搜就一大堆,今天我們來討論的這款特效,代碼卻很簡潔,兼容性也非常棒,推薦給大家。2014-12-12今天抽時間給大家整理jquery和ajax的相關(guān)知識
jquery ajax2015-11-11