What's the difference between '$(this)' and 'this'?

前端 未结 7 1573
面向向阳花
面向向阳花 2020-11-21 07:18

I am currently working through this tutorial: Getting Started with jQuery

For the two examples below:

$(\"#orderedlist\").find(\"li\").each(function          


        
7条回答
  •  执念已碎
    2020-11-21 07:48

    this reference a javascript object and $(this) used to encapsulate with 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')
    

提交回复
热议问题