JQuery中$(this)与this的区别
本文最后更新于 3580 天前,其中的信息可能已经有所发展或是发生改变。

因为今晚做了一道JQuery的题目,我记得this是DOM对象,而$(this)是JQ对象

但是我自己不确定于是就度娘了一下,毕竟很多刚接触JQuery的人,都会对$(this)和this的区别模糊不清,我也一样嘛。(新手我路过!)

那么this和$(this)两者有什么区别呢?

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

如:

$(function(){
    $('#switcher-default').click(function(){
       alert($(this))
    });
});

//弹出的是[Object Object]

$(function(){
    $('#switcher-default').click(function(){
       alert(this)
    });
});

//弹出的是[object HTMLImageElement]


所以说,this是返回HTML对象,那么,HTML对象中怎么会有val()方法了,因此我们在使用中,不能直接用this来直接调用jquery的方法或者属性。(因为他们不是兄弟吧)

这是自己的一些JQuery的笔记。希望也对新手们有用。么么哒

评论

  1. Image
    Linux Chrome 54.0.2840.59
    10 年前
    2016-10-30 18:52:39

    我居然从没想过这个问题。

    • Image
      博主
      不亦乐乎
      Windows Chrome 52.0.2743.116
      10 年前
      2016-10-31 21:18:43

      因为我是新生,你是老师-

  2. Image
    Macintosh Chrome 54.0.2840.71
    10 年前
    2016-11-01 10:07:09

    还有 $(this)、$(this)[0] 和 this 的区别

    • Image
      博主
      麦葱
      Windows Chrome 52.0.2743.116
      10 年前
      2016-11-01 12:44:39

      $(this)[0]是否等于$(this).get(0)?

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇