国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > JavaScript > 正文

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

2024-05-06 16:13:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了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ì)有所幫助。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 泌阳县| 湄潭县| 绥棱县| 息烽县| 大理市| 黄山市| 呼和浩特市| 肥乡县| 通城县| 勃利县| 安康市| 囊谦县| 云龙县| 板桥市| 漳州市| 南木林县| 长海县| 铁岭县| 舞阳县| 宿松县| 绥棱县| 昭苏县| 中超| 永昌县| 江孜县| 彰武县| 曲靖市| 天门市| 视频| 安义县| 晋州市| 闽清县| 闻喜县| 陆丰市| 景谷| 清原| 娄烦县| 吐鲁番市| 德惠市| 金华市| 克什克腾旗|