JQuery and 'this' object

后端 未结 5 2064
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 00:14

I have the following JQuery function being attached to the blur event of n textboxes on a webpage.

$(document).ready(function() {
        $(\"input[id$=\'_tx         


        
5条回答
  •  执笔经年
    2021-01-03 00:49

    this refers to the current dom object (the same way ie document.getElementById("someid") refers to the wanted dom object). Based on the browser you can now access functions/field of that object (ie. this.nodeName, this.value, ...) You are accessing what is provided by the browser's implementation.

    If you use $(this) (or $("#someid") or $(document.getElementById("someid"))) You are ecapsulating the object in jquery - thus you can now access the jquery functions and fields (ie. $(this).val(); $(this).find("somenode"), ....)

    If you have a jquery object (i.e. var n = $(this).find("#someid"); ) and you want to get rid of the jquery capsule, because you need a standard dom function you get use .get(0).

    this itself can resolve to different objects, depending on where it is called. It can be a node if called within an onclick or other event handler (

提交回复
热议问题