代码之家  ›  专栏  ›  技术社区  ›  penguru

$(this)与jQuery中的this

  •  5
  • penguru  · 技术社区  · 14 年前

    两者有什么区别 $(this) this 在jQuery中?这里有两种不同的用法:

     $(document).ready(function() {
       $("#orderedlist").find("li").each(function(i) {
         $(this).append( " BAM! " + i );
       });
     });
    
    
     $(document).ready(function() {
       // use this to reset several forms at once
       $("#reset").click(function() {
         $("form").each(function() {
           this.reset();
         });
       });
     });
    
    3 回复  |  直到 14 年前
        1
  •  10
  •   Pointy    14 年前

    “this”变量(在您提供的事件处理程序中)引用DOM元素。因此$(this)是只包含一个DOM元素的jQuery对象。

    当本机domapi足够时,应该使用普通的“this”,当需要jQuery帮助时,应该使用$(this)。你的第二个例子是一个很好的例子;另一种可能是您只需要元素的“id”或“name”。

        2
  •  9
  •   spinon    14 年前

    不同的是 this 它本身是对事件所作用的dom对象的引用。 $(this)

    EDIT:因此使用DOM对象,您将无法访问jquery添加的所有功能,而只能访问DOM允许的功能。基本上你能想到 就像你做的一样 document.getElementById(id)

        3
  •  8
  •   Giorgi    14 年前

    $(this) 是jquery对象,而 this