jquery attr() method


  Translation results:

attr

UK ['ətr] US ['ətr]

abbr.attractive attractive, charming

jquery attr() methodsyntax

Function: attr() method sets or returns the attribute value of the selected element. Depending on the parameters of the method, the way it works is also different.

Return attribute value: Return the attribute value of the selected element.

Syntax: $(selector).attr(attribute

##Parameters:

ParameterDescriptionattribute Specifies the attribute whose value is to be obtained.

Set attributes/values: Set the attributes and values ​​of the selected element.

Syntax: $(selector).attr(attribute, value

Parameters:

ParameterDescriptionattribute Specifies the name of the attribute. value Specifies the value of the attribute.

Use functions to set attributes/values: Set the attributes and values ​​of the selected elements.

Syntax: $(selector).attr(attribute,function (index,oldvalue))

Parameters:

##Parametersattribute function(index,oldvalue)
Description
Specifies the name of the attribute.
Specifies the function that returns the attribute value . This function can receive and use the index value of the selector and the current attribute value.
Set multiple attribute/value pairs:

is the selected element Set more than one attribute and value.

Syntax:

$(selector).attr({attribute:value, attribute:value ...})

Parameters:

Parametersattribute:value

jquery attr() methodexample

<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>设置图像的 width 属性</button>
</body>
</html>
Run instance »

Click the "Run instance" button to view the online instance

Description
Specify one or more attribute/value pairs.