首页 文章

什么's the difference between ' $(这)' and '这个'?

提问于
浏览
532

我目前正在完成本教程:Getting Started with jQuery

对于以下两个示例:

$("#orderedlist").find("li").each(function (i) {
    $(this).append(" BAM! " + i);
});
$("#reset").click(function () {
    $("form").each(function () {
        this.reset();
    });
});

请注意,在第一个示例中,我们使用 $(this) 在每个 li 元素中附加一些文本 . 在第二个示例中,我们在重置表单时直接使用 this .

$(this) 似乎比 this 使用频率更高 .

我的猜测是在第一个例子中, $() 正在将每个 li 元素转换为理解 append() 函数的jQuery对象,而在第二个示例中, reset() 可以直接在窗体上调用 .

基本上我们需要 $() 用于特殊的jQuery-only函数 .

它是否正确?

7 回答

  • 43

    使用 jQuery 时,建议通常使用 $(this) . 但如果你知道(你应该学习并知道)差异,有时候使用 this 会更方便,更快捷 . 例如:

    $(".myCheckboxes").change(function(){ 
        if(this.checked) 
           alert("checked"); 
    });
    

    比起来更简单,更纯粹

    $(".myCheckboxes").change(function(){ 
          if($(this).is(":checked")) 
             alert("checked"); 
    });
    
  • 89

    this 是元素, $(this) 是使用该元素构造的jQuery对象

    $(".class").each(function(){
     //the iterations current html element 
     //the classic JavaScript API is exposed here (such as .innerHTML and .appendChild)
     var HTMLElement = this;
    
     //the current HTML element is passed to the jQuery constructor
     //the jQuery API is exposed here (such as .html() and .append())
     var jQueryObject = $(this);
    });
    

    更深入的了解

    thisMDN 包含在执行上下文中

    范围是指当前的 Execution ContextECMA . 为了理解 this ,理解执行上下文在JavaScript中的运行方式非常重要 .

    执行上下文绑定此

    当控制进入执行上下文(代码正在该范围内执行)时,变量的环境被设置(词法和变量环境 - 实际上这为要输入的变量设置了一个区域,这些区域已经可访问,并且局部变量的区域是存储),并发生 this 的绑定 .

    jQuery绑定了这个

    执行上下文形成逻辑堆栈 . 结果是堆栈中更深层次的上下文可以访问先前的变量,但它们的绑定可能已被更改 . Every time jQuery calls a callback function, it alters the this binding 使用 applyMDN .

    callback.apply( obj[ i ] )//where obj[i] is the current element
    

    调用 apply 的结果是回调函数使用 inside of jQuery callback functions, this refers to the current element .

    例如,在 .each 中,常用的回调函数允许 .each(function(index,element){/*scope*/}) . 在该范围内, this == element 是真的 .

    jQuery回调使用apply函数将正在调用的函数与当前元素绑定 . 该元素来自jQuery对象的元素数组 . 构造的每个jQuery对象都包含一个元素数组,这些元素与用于实例化jQuery对象的 selectorjQuery API 匹配 .

    $(selector) 调用jQuery函数(记住 $ 是对 jQuery 的引用,代码: window.jQuery = window.$ = jQuery; ) . 在内部,jQuery函数实例化一个函数对象 . 因此虽然它可能不是很明显,但在内部使用 $() 使用 new jQuery() . 构建此jQuery对象的一部分是查找选择器的所有匹配项 . 构造函数也将接受html字符串和元素 . When you pass this to the jQuery constructor, you are passing the current element for a jQuery object to be constructed with . 然后,jQuery对象包含与选择器匹配的DOM元素的类似数组的结构(或者在 this 的情况下只包含单个元素) .

    构建jQuery对象后,现在公开了jQuery API . 当调用jQuery api函数时,它将在内部迭代这个类似数组的结构 . 对于数组中的每个项,它调用api的回调函数,将回调的 this 绑定到当前元素 . 这个调用可以在上面的代码片段中看到,其中 obj 是类似于数组的结构, i 是用于当前元素数组中位置的迭代器 .

  • 357

    this 引用javascript对象, $(this) 用于封装jQuery .

    Example =>

    // Getting Name and modify css property of dom object through jQuery
    var name = $(this).attr('name');
    $(this).css('background-color','white')
    
    // Getting form object and its data and work on..
    this = document.getElementsByName("new_photo")[0]
    formData = new FormData(this)
    
    // Calling blur method on find input field with help of both as below
    $(this).find('input[type=text]')[0].blur()
    
    //Above is equivalent to
    this = $(this).find('input[type=text]')[0]
    this.blur()
    
    //Find value of a text field with id "index-number"
    this = document.getElementById("index-number");
    this.value
    
    or 
    
    this = $('#index-number');
    $(this).val(); // Equivalent to $('#index-number').val()
    $(this).css('color','#000000')
    
  • 38

    $() 是jQuery构造函数 .

    this 是对调用的DOM元素的引用 .

    基本上,在 $(this) 中,您只是将 this 中的 this 作为参数传递,以便您可以调用jQuery方法和函数 .

  • 491

    是的,通过使用 $(this) ,您为对象启用了jQuery功能 . 通过使用 this ,它只具有通用的Javascript功能 .

  • -2

    是的,你需要 $(this) 用于jQuery函数,但是当你想要访问不使用jQuery的元素的基本javascript方法时,你可以只使用 this .

  • 70

    是的,你只需要 $() ,当你帮助做DOM事情时,请记住这一点 .

    $(this)[0] === this
    

    基本上每当你得到一组元素时,jQuery就会把它变成jQuery object . 如果你知道你只有一个结果,它将在第一个元素中 .

    $("#myDiv")[0] === document.getElementById("myDiv");
    

    等等...

相关问题