<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>设置属性和获取属性</title>
<script src="js/jquery-1.7.1.js"></script>
<script>
$(function(){
alert($('.box').attr('class'));//获取属性值;为box;
$('.box').attr('title','nihao');//设置属性的属性值;
$('.box').attr({
'title':'nihao',
'class': 'red',//class不建议用attr来创建,后面有更强大的功能;
'data':'123' //最后一个不用逗号
});
$('.box').attr('title',function(index,value) {//如果原先没有title,那么返回 //undefind;
// index是索引,从0开始
return '原来的title是' + value +',现在的title是' + '我是' + index + '号';
});
var arr = ['haha','hahahahaha','yaya'];
$.each(arr, function(index,value) {
return $('p').text(value);
});
$('p').text(function(index,value) {
return arr[index];
return value + ' + ' + arr[index % arr.length];
});
});
</script>
</head>
<body>
<div class="box1" title="aaa"><p>hello</p></div>
<div class="box2" title="bbb"><p>world</p></div>
<div class="box1" title="aaa"><p>yu</p></div>
<div class="box2" title="bbb"><p>world</p></div>
</body>
</html>