最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery中attr()方法用法實例

 更新時間:2015年01月05日 09:17:42   投稿:shichen2014  
這篇文章主要介紹了jQuery中attr()方法用法,實例分析了attr()方法的功能、定義及設(shè)置或返回匹配元素屬性值的使用技巧,需要的朋友可以參考下

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

此方法設(shè)置或返回匹配元素的屬性值。
attr()方法根據(jù)參數(shù)的不同,功能也不同。

語法結(jié)構(gòu)一:
獲取第一個匹配元素指定屬性的屬性值。

復制代碼 代碼如下:
$(selector).attr(name)

參數(shù)列表:

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

實例代碼:

復制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.fzitv.net/" />
<title>attr()函數(shù)-腳本之家</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">我是第一個div</div>
<div class="second">我是第二個div</div>
<button id="btn">點擊查看效果</button>
</body>
</html>

以上代碼中,點擊按鈕可以彈出匹配元素集合中,第一個元素的class屬性值。

語法結(jié)構(gòu)二:
為匹配元素指定的屬性設(shè)置屬性值。

復制代碼 代碼如下:
$(selector).attr(attribute,value)

參數(shù)列表:

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

實例代碼:

復制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.fzitv.net/" />
<title>attr()函數(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").attr("class","reset");
  });
})
</script>
</head>
<body>
<div class="font bg">腳本之家歡迎您</div>
<button id="btn">點擊查看效果</button>
</body>
</html>

以上代碼中, 當點擊按鈕的時候,能夠重新設(shè)置div元素的class屬性值。

語法結(jié)構(gòu)三:
將“名/值”形式的對象設(shè)置為匹配元素的屬性??梢砸淮涡栽O(shè)置多組“名/值”對,對匹配元素屬性進行設(shè)置。

復制代碼 代碼如下:
$(selector).attr(properties)

參數(shù)列表:

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

實例代碼:

復制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.fzitv.net/" />
<title>attr()函數(shù)-腳本之家</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>歡迎來到腳本之家</td>
</tr>
</table>
<button id="btn">點擊查看效果</button>
</body>
</html>

以上代碼中,可以設(shè)置單元格的寬度和高度。

語法結(jié)構(gòu)四:通過函數(shù)返回值設(shè)置屬性值。

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

參數(shù)列表:

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

實例代碼:

復制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.fzitv.net/" />
<title>attr()函數(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").attr("class" ,function(){
      return "reset"
    })
  })
})
</script>
</head>
<body>
<div class="font bg">腳本之家歡迎您</div>
<button id="btn">點擊查看效果</button>
</body>
</html>

以上代碼中,同樣可以設(shè)置div的class屬性值,只是屬性值是以函數(shù)的返回值方式獲得的。

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

相關(guān)文章

最新評論

阿城市| 共和县| 庄河市| 弋阳县| 绵竹市| 巴林左旗| 南阳市| 巩留县| 丽水市| 双辽市| 巴东县| 南投市| 蓬莱市| 景德镇市| 吉水县| 准格尔旗| 漯河市| 东丰县| 望城县| 宜君县| 韶关市| 舟曲县| 临猗县| 遂溪县| 得荣县| 岫岩| 邢台市| 武山县| 平昌县| 巨野县| 来凤县| 锡林浩特市| 昌吉市| 波密县| 太谷县| 陵水| 怀化市| 沅江市| 梨树县| 永泰县| 河间市|