attr

英 ['?tr]? ?美 ['?tr]??

abbr.attractive 吸引人的,有魅力的

jquery attr() 方法 語(yǔ)法

作用:attr() 方法設(shè)置或返回被選元素的屬性值。根據(jù)該方法不同的參數(shù),其工作方式也有所差異。

返回屬性值:返回被選元素的屬性值。

語(yǔ)法:$(selector).attr(attribute

參數(shù):

參數(shù)描述
attribute? ??規(guī)定要獲取其值的屬性。

設(shè)置屬性/值:設(shè)置被選元素的屬性和值。

語(yǔ)法:$(selector).attr(attribute,value

參數(shù):

參數(shù)描述
attribute? ??規(guī)定屬性的名稱(chēng)。
value? ??規(guī)定屬性的值。?

使用函數(shù)設(shè)置屬性/值:設(shè)置被選元素的屬性和值。

語(yǔ)法:$(selector).attr(attribute,function(index,oldvalue))

參數(shù):

參數(shù)描述
attribute? ??規(guī)定屬性的名稱(chēng)。
function(index,oldvalue)規(guī)定返回屬性值的函數(shù)。該函數(shù)可接收并使用選擇器的 index 值和當(dāng)前屬性值。

設(shè)置多個(gè)屬性/值對(duì):為被選元素設(shè)置一個(gè)以上的屬性和值。

語(yǔ)法:$(selector).attr({attribute:value, attribute:value ...})

參數(shù):

參數(shù)描述
attribute:value規(guī)定一個(gè)或多個(gè)屬性/值對(duì)。

jquery attr() 方法 示例

<html>
<head>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("img").attr("width","180");
  });
});
</script>
</head>
<body>
<img src="http://img.php.cn/upload/article/000/005/656/5af270fd37755429.jpg" />
<br />
<button>設(shè)置圖像的 width 屬性</button>
</body>
</html>
運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例