Heim  >  Artikel  >  Web-Frontend  >  jQuery中 attr() 方法使用小结_jquery

jQuery中 attr() 方法使用小结_jquery

WBOY
WBOYOriginal
2016-05-16 16:01:391198Durchsuche

定义和用法

attr() 方法设置或返回被选元素的属性值。

根据该方法不同的参数,其工作方式也有所差异。

实例1

设置被选元素的属性和值。

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("img").attr("width","180");
});
});
</script>
</head>
<body>
<img  src="/i/eg_smile.gif" / alt="jQuery中 attr() 方法使用小结_jquery" >
<br />
<button>设置图像的 width 属性</button>
</body>
</html>

实例2

返回被选元素的属性值。

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
alert("Image width " + $("img").attr("width"));
});
});
</script>
</head>
<body>
<img  src="/i/eg_smile.gif"    style="max-width:90%"  style="max-width:90%" / alt="jQuery中 attr() 方法使用小结_jquery" >
<br />
<button>返回图像的宽度</button>
</body>
</html>

以上所述就是本文的全部内容了,希望大家能够喜欢。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn