$(this)vs jQuery中的这个 [英] $(this) vs this in jQuery

查看:93
本文介绍了$(this)vs jQuery中的这个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery中 $(this)这个有什么区别?以下是两种不同的用法:

What is the difference between $(this) and this in jQuery ? Here are two different usage :

 $(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();
     });
   });
 });


推荐答案

this变量指(在这种情况下为您提供的事件处理程序)到DOM元素。因此$(this)是一个只包含一个DOM元素的jQuery对象。

The "this" variable refers (in such cases as the event handlers you've supplied) to a DOM element. Thus $(this) is a jQuery object containing just that one DOM element.

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

You should use plain "this" when the native DOM APIs suffice, and $(this) when you need the help of jQuery. Your second example is a good illustration; another might be when you just need the "id" or "name" of an element.

这篇关于$(this)vs jQuery中的这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆