码迷,mamicode.com
首页 > Web开发 > 详细

jQuery中this与$(this)的区别

时间:2015-08-30 08:43:52      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <link href="style/css.css" rel="stylesheet" type="text/css" />            

        <script src="js/jquery-1.7.2.min.js"></script>        
        <script>
            $(document).ready(function(){
                console.log("ready");
                $("#textbox").hover(
                    function(){
                        console.log("fdsfds");
                        this.title="Test";
                    },
                    function(){
                        this.title="OK";
                    }
                );
            });
        </script>
        
    </head>
    <body>
        <p id="textbox">pppppppppppp</p>
    </body>
</html>

这里的this其实是一个Html 元素(textbox),textbox有title属性,所以这样写是完全没有什么问题的。
但是如果将this换成$(this)就不是那回事了,Error--报了。$(this)表示一个jquery对象。this与$(this)的区别在此。

要用$(this),得这样写。

$("#textbox").hover(   
      function() {   
         $(this).attr(’title’, ‘Test’);   
      },   
      function() {   
         $(this).attr(’title’, ‘OK’);   
      }   
); 

 

jQuery中this与$(this)的区别

标签:

原文地址:http://www.cnblogs.com/as3lib/p/4770290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!