版权声明:本文为大都督作者的原创文章,未经 大都督 允许也可以转载,但请注明出处,谢谢! 共勉! https://blog.csdn.net/qq_37335220/article/details/85198204
1.效果图
2.html代码
<!DOCTYPE html>
<html>
<head>
<title>使用each()方法遍历元素获取属性</title>
<style type="text/css">
body{font-size: 13px}
img{border: solid 1px #ccc;padding: 3px;margin: 5px;width: 143px;height: 101px;}
p{background-color: #eee;padding: 5px;width: 200px;}
</style>
</head>
<body>
<img alt="第0只小猪" src="../img/pig.jpg">
<img alt="第1只小猪" src="../img/pig.jpg">
<img alt="第2只小猪" src="../img/pig.jpg">
<script src="../jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("img").each(function(index){
//根据形参index设置元素的title属性
this.title = "第"+index+"只小猪,alt内容是"+this.alt;
})
});
</script>
</body>
</html>