欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery中prop()方法用法實(shí)例

 更新時(shí)間:2015年01月05日 10:24:06   投稿:shichen2014  
這篇文章主要介紹了jQuery中prop()方法用法,以實(shí)例形式較為詳細(xì)的分析了prop()方法的功能、定義及獲取或者設(shè)置匹配元素屬性值的各種常見使用技巧,需要的朋友可以參考下

本文實(shí)例講述了jQuery中prop()方法用法。分享給大家供大家參考。具體分析如下:

此方法可以獲取或者設(shè)置匹配元素的屬性值。
根據(jù)方法參數(shù)的不同,作用也有所不同。

語法結(jié)構(gòu)一:
當(dāng)參數(shù)為屬性名稱時(shí),此方法能夠匹配元素集合中,第一個(gè)匹配元素指定屬性名稱的屬性值。

復(fù)制代碼 代碼如下:
$("selector").prop(name)

參數(shù)列表:

參數(shù) 描述
name 定義要獲取其值的屬性名稱。

實(shí)例代碼:

實(shí)例一:

復(fù)制代碼 代碼如下:

<!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的屬性值。

實(shí)例代碼二:

復(fù)制代碼 代碼如下:

<!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元素集合中第一個(gè)li元素并沒有id屬性,所以返回值為空。

語法結(jié)構(gòu)二:

以屬性的“名/值對(duì)”對(duì)象方式設(shè)置所有匹配元素的屬性值。

復(fù)制代碼 代碼如下:
$(selector).prop(properties)

參數(shù)列表:

參數(shù) 描述
attribute:value 定義屬性名/值對(duì)

實(shí)例代碼:

實(shí)例一:

復(fù)制代碼 代碼如下:

<!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ù)選框。

實(shí)例二:

復(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的寬度和高度。

語法三:

以屬性名/值對(duì)方式設(shè)置所有匹配元素的屬性值。

復(fù)制代碼 代碼如下:
$(selector).prop(key,value)

參數(shù)列表:

參數(shù) 描述
key 定義要設(shè)置值的屬性名稱。
value 定義要設(shè)置的屬性值。

實(shí)例代碼:

復(fù)制代碼 代碼如下:

<!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">點(diǎn)擊查看效果</button>
</body>
</html>

以上代碼可以為div設(shè)置指定的樣式。

語法結(jié)構(gòu)四:

通過函數(shù)返回值設(shè)置屬性值。

復(fù)制代碼 代碼如下:
$(selector).prop(name,function(index,oldvalue))

參數(shù)列表:

參數(shù) 描述
name 定義要設(shè)置值的屬性的名稱。
function(index,oldvalue) 定義返回屬性值的函數(shù)
index - 可選,接受選擇器的索引位置。
class - 可選,接受選擇器的當(dāng)前的屬性值。

實(shí)例代碼:

復(fù)制代碼 代碼如下:

<!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">點(diǎn)擊查看效果</button>
</body>
</html>

以上代碼可以為div設(shè)置指定的樣式。

希望本文所述對(duì)大家的jQuery程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論