$this与this

2017-05-15  本文已影响0人  zigzh

仅代表个人看法

看了一些关于jQuery中this和javaScirpt的this的区别,描述都不甚明了,很虐新。
个人感觉是两种对象分别调取jQuery和javaScript的函数。
$this能调取jQuery类库的函数,jQuery函数更加的简洁,兼容性更好。
当然,this对象可以通过$(this)转换成jQuery对象。

    <script type="text/javascript">
        var p1 = document.getElementById('test1')
        p1.addEventListener('click',function(){
            //直接通过dom的方法改变颜色
            this.style.color = "red"; 
        },false);
    </script>

    <script type="text/javascript">
        $('#test2').click(function(){
            //通过包装成jQuery对象改变颜色
            $(this).css('color','blue');
        })
    </script>

jQuery的方法更加简洁,好用。兼容性也会相对更好一些。

请各位大神指正。

上一篇 下一篇

猜你喜欢

热点阅读