为什么不能用$(this)

来源:1-11 编程练习

暮女神

2017-07-11 20:59

如果用$(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>


写回答 关注

3回答

  • Frank_Yuan
    2017-09-04 03:22:15
    1. $(this) 返回一个Jq对象

    2. $(this)[0].name 就能访问到name

  • Boson_Sun
    2017-07-11 23:06:00

    我说错了,不好意思啊 , each中this和e相同,jquery内部处理的,你再把他处理成jquery以后就不能直接点语法访问到name了

    暮女神

    那应该怎么访问到name呢

    2017-07-12 00:21:24

    共 1 条回复 >

  • Boson_Sun
    2017-07-11 22:59:35

    each函数中this已经被jQuery处理过了,已经是jquery对象了

jQuery基础(五)一Ajax应用与常用插件

如何用jquery实现ajax应用,加入学习,有效提高前端开发速度

69095 学习 · 400 问题

查看课程

相似问题