整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

jquery中的$(this)与this的区别(你真

jquery中的$(this)与this的区别(你真的了解吗)

信很多刚接触JQuery的人,很多都会对$(this)和this的区别模糊不清,那么这两者有什么区别呢?

首先来看看JQuery中的 $() 这个符号,实际上这个符号在JQuery中相当于JQuery(),即$(this)=jquery();也就是说,这样可以返回一个jquery对象。那么,当你在网页中alert($('#id'));时,会弹出一个[object Object ],这个object对象,也就是jquery对象了。

那么,我们再回过头来说$(this),这个this是什么呢?假设我们有如下的代码:

$("#desktop a img").each(function(index){

alert($(this));

alert(this);

}

那么,这时候可以看出来:

alert($(this)); 弹出的结果是[object Object ]

alert(this); 弹出来的是[object HTMLImageElement]

也就是说,后者返回的是一个html对象(本例中是遍历HTML的img对象,所以为 HTMLImageElement)。很多人在使用jquery的时候,经常this.attr('src'); 这时会报错“对象不支持此属性或方法”,这又是为什么呢?其实看明白上面的例子,就知道错在哪里了:

很简单,this操作的是HTML对象,那么,HTML对象中怎么会有val()方法了,所以,在使用中,我们不能直接用this来直接调用jquery的方法或者属性。

.JQuery

JQuery 是将 JS 的一些代码块进行封装,方便使用。

1.JQ的引入

(1)link 导入

先进入 https://www.bootcdn.cn/ 网站进行查找,找到后复制到一个 js 中,进行引用。

(2)直接复制标签

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>

二.JQ JS 相互转换

1. JQ 获取元素

$('.p1').eq(1).text('今天天气真好')
    $('.p1').html('<h>天气真热</h>')

2.JS 转 JQ

$(ap1).text('天好冷')

3. JQ 转 JS

	var ap3=$('.p1')
    ap3[0].innerText='金地是'
    ap3.get(1).innerText='多少金币' //get() 传下标

4. JQ JS 都可用

$('ul li').each(function (){
        console.log($(this).text());
        // console.log(this.innerText);
        console.log($(this).index()); //jq 获取下标

    })

三.JQ 操作 HTML 属性

<button>添加</button>
<button>删除</button>

1. 添加 class

//添加class
    $("button").eq(0).click(function (){
        $("div").addClass("div1")
    })

2. 删除 class

(1)removeClass

//删除class
    $("button").eq(1).click(function (){
        $("div").removeClass("div1")
    })

(2)removeAttr

//删除属性和属性值
    $("button").eq(1).click(function (){
$("div").removeAttr("class")

3. 修改 class

(1)toggleclass

//无则增 有则增
 $("button").eq(0).click(function (){
 	$("div").toggleClass("div1")

(2)attr

//无则增 有则改
$("button").eq(0).click(function (){
        $("div").attr("class","div1")
        $("div").attr("class","div2")
    })

4.获取 value

$("input").eq(0).val('666');

四.JQ 操作 CSS 样式

1. 获取盒子宽高

(1)获取宽

console.log($("div").width());

(2)获取内边框加宽

$("div").innerWidth()

(3)获取内边框,边框外边距和宽的宽度

$("div").outerWidth()

2. JQ 修改 CSS

// jq修改css
   $("div").css("background","blue")
   $("div").css({
       "background":"pink",
       "width":"150px"
   })

3.定位元素(父级元素一定要有定位)

$(".div2").position()

4.定位浏览器窗口

$(".div2").offset()

五. JQ 事件

1.单击事件

$("div").click(function (){
        console.log(1);
    })

2.双击事件

$("div").dblclick(function (){
        console.log(2);
    })

3.划入事件

$("div").mouseenter(function (){
        console.log(3);
    })

4.划出事件

$("div").mouseout(function (){
        console.log(4);
    })

5.划入划出事件

$("div").hover(
        function (){
            console.log(3);
        },function (){
            console.log(5);
        }
    )

6.绑定事件

$("button").click(function (){
        $("p").on("click",function (){
            $("p").css('background','red')
        })
    })

7.绑定多个事件

$("p").on({
         "mouseenter":function (){
             $(this).css('background','yellow')
         },
         "mouseout":function (){
             $(this).css('background','blue')
         }
     })

8.清除事件

$("button").click(function (){
        $("p").off()
    })

六. JQ 动画

1. 隐藏

$("button").eq(0).click(function (){
        // $("div").hide(1000)
        $("div").slideUp(1000)

    })

2.显示

$("button").eq(1).click(function (){
        $("div").show(1000)
        // $("div").slideDown(1000)
    })

3.取反

$("button").eq(2).click(function (){
        $("div").slideToggle(1000)
        // $("div").slideDown(1000)
    })

4.淡出事件

$("button").eq(3).click(function (){
        $("div").fadeOut(1000)
    })

5.淡入事件

$("button").eq(4).click(function (){
        $("div").fadeIn(1000)
    })

6.淡入淡出取反事件

$("button").eq(5).click(function (){
        $("div").fadeToggle(1000)
    })

7.动画效果

$("button").eq(6).click(function (){
        $("div").delay(100).animate({
            "width":"130px",
            "height":"130px",
            "top":"50px",
            "left":"20px",
        })
    })

8.停止

$("button").eq(7).click(function (){
        $("div").stop(1000)
    })

附(今日份学习):

Query中this与$(this)的区别

$("#textbox").hover(
    function() {
        this.title="Test";
    },
    fucntion() {
        this.title="OK";
    }
);

这里的this其实是一个HTML元素(#textbox), 元素(#textbox)有title属性, 所以这样写是完全没有什么问题的。

但是如果将this换成$(this)就不是那回事了, Error--报了。this与$(this)的区别在此。

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

这里的$(this)是一个jQuery对象, 而jQuery对象沒有title 属性, 因此这样写是错误的。

JQuery拥有attr()方法可以get/set DOM对象的属性,所以正确的写法应该是这样:

正确的代码:

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

使用jQuery的好处是它包裝了各种浏览器版本对DOM对象的操作,因此统一使用$(this)而不再用this应该是比较不错的选择。