jQuery中prop()方法用法實(shí)例
本文實(shí)例講述了jQuery中prop()方法用法。分享給大家供大家參考。具體分析如下:
此方法可以獲取或者設(shè)置匹配元素的屬性值。
根據(jù)方法參數(shù)的不同,作用也有所不同。
語(yǔ)法結(jié)構(gòu)一:
當(dāng)參數(shù)為屬性名稱時(shí),此方法能夠匹配元素集合中,第一個(gè)匹配元素指定屬性名稱的屬性值。
參數(shù)列表:
實(shí)例代碼:
實(shí)例一:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://m.fzitv.net/" />
<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的屬性值。
實(shí)例代碼二:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://m.fzitv.net/" />
<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元素集合中第一個(gè)li元素并沒(méi)有id屬性,所以返回值為空。
語(yǔ)法結(jié)構(gòu)二:
以屬性的“名/值對(duì)”對(duì)象方式設(shè)置所有匹配元素的屬性值。
參數(shù)列表:
實(shí)例代碼:
實(shí)例一:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://m.fzitv.net/" />
<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ù)選框。
實(shí)例二:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://m.fzitv.net/" />
<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>歡迎來(lái)到腳本之家</td>
</tr>
</table>
</body>
</html>
以上代碼可以設(shè)置td的寬度和高度。
語(yǔ)法三:
以屬性名/值對(duì)方式設(shè)置所有匹配元素的屬性值。
參數(shù)列表:
實(shí)例代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://m.fzitv.net/" />
<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">點(diǎn)擊查看效果</button>
</body>
</html>
以上代碼可以為div設(shè)置指定的樣式。
語(yǔ)法結(jié)構(gòu)四:
通過(guò)函數(shù)返回值設(shè)置屬性值。
參數(shù)列表:
實(shí)例代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://m.fzitv.net/" />
<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">點(diǎn)擊查看效果</button>
</body>
</html>
以上代碼可以為div設(shè)置指定的樣式。
希望本文所述對(duì)大家的jQuery程序設(shè)計(jì)有所幫助。
- jquery獲取自定義屬性(attr和prop)實(shí)例介紹
- jquery中prop()方法和attr()方法的區(qū)別淺析
- js與jquery實(shí)時(shí)監(jiān)聽輸入框值的oninput與onpropertychange方法
- jQuery學(xué)習(xí)之prop和attr的區(qū)別示例介紹
- jquery下onpropertychange事件的綁定方法
- Jquery阻止事件冒泡 event.stopPropagation
- jquery 獲取自定義屬性(attr和prop)的實(shí)現(xiàn)代碼
- jQuery中attr()和prop()在修改checked屬性時(shí)的區(qū)別
- jQuery獲取attr()與prop()屬性值的方法及區(qū)別介紹
- 詳解jQuery中的prop()使用方法
相關(guān)文章
jQuery事件之鍵盤事件(ctrl+Enter回車鍵提交表單等)
鍵盤事件處理所有用戶在鍵盤敲擊的情況,不管在文本輸入?yún)^(qū)域內(nèi)部還是外部,這里介紹下jquery鍵盤事件的相關(guān)知識(shí),需要的朋友可以參考下2014-05-05
jQuery實(shí)現(xiàn)彈窗居中效果類似alert()
本文給大家分享基于jquery實(shí)現(xiàn)彈窗居中效果類似于alert(),代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-02-02
加載列表時(shí)jquery獲取ul中第一個(gè)li的屬性
通過(guò)jquery獲取ul中第一個(gè)li的屬性,當(dāng)加載列表時(shí),默認(rèn)希望選中第一條,下面是具體的實(shí)現(xiàn)代碼2014-11-11
Jquery Ajax學(xué)習(xí)實(shí)例3 向WebService發(fā)出請(qǐng)求,調(diào)用方法返回?cái)?shù)據(jù)
Jquery Ajax學(xué)習(xí)實(shí)例3 向WebService發(fā)出請(qǐng)求,調(diào)用方法返回?cái)?shù)據(jù)2010-03-03
jquery入門—編寫一個(gè)導(dǎo)航條(可伸縮)
編寫一個(gè)導(dǎo)航條,單擊標(biāo)題時(shí),可以伸縮導(dǎo)航條內(nèi)容,簡(jiǎn)化內(nèi)容或顯示更多內(nèi)容等等效果相當(dāng)不錯(cuò),感興趣的朋友可以了解下哦2013-01-01

