如果用$(this).name就会返回undefined,如果用this.name就正常显示,这是为何?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>挑战题</title>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<ul id="result" style="list-style:none"></ul>
<script type="text/javascript">
$(document).ready(function(){
var json=[
{"name":"小明","sex":"男","age":14,"married":false}
,{"name":"小红","sex":"女","age":11,"married":false}
,{"name":"小刚","sex":"男","age":16,"married":true}
];
$.each(json,function(index,e){
$("#result").append("<li>姓名:"+$(this).name+",性别:"+e.sex+",年龄:"+e.age+",已婚:"+e.married+"</li>");
});
});
</script>
</body>
</html>$(this) 返回一个Jq对象
$(this)[0].name 就能访问到name
这里调用的是DOM对象啊,e就相当于this选择器
我说错了,不好意思啊 , each中this和e相同,jquery内部处理的,你再把他处理成jquery以后就不能直接点语法访问到name了
each函数中this已经被jQuery处理过了,已经是jquery对象了