jQuery中this与$(this)的区别

<!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’);   
      }   
); 

 一个移动设备触控滑块的js很不错的样子:

http://www.swiper.com.cn/

posted on 2015-08-30 07:55  防空洞123  阅读(220)  评论(0编辑  收藏  举报

导航