WEB difference between front-end development and study notes --this $ (this) of

I believe that many people new to JQuery, many will be on the difference between $ (this) and this blurred, then both what difference does it?

First look in JQuery $ () symbol, the symbol actually corresponds JQuery () in JQuery, i.e. $ (this) = jquery (); That is, such a return may jquery object.

Usually we directly use the $ for simplicity (). In practice, the function omit a parameter context. Selected according to the selection object matching i.e. $ (selector, context), returns in the form of packaging jQuery set.

Dom context can be a collection of objects or jQuery package set, it means passing from context select a matching object, not pass the bounds document object (that is, all page objects), that is, $ (selector) = $ (selector, document).

this means that the object function call that html.

this, indicates that the current context object is a html objects, properties and methods of objects owned html can call.

$ (The this), the context object represents a jquery context object can call the methods and property values ​​of jquery. For example this.style.display = "none" can be implemented in jquery $ (this) .css ( "display", "none").

Suppose we have the following code:

$("#desktop a img").each(function(index){

          alert($(this));

          alert(this);

}

Well, this time can be seen:

alert ($ (this)); pop result [object Object]

alert (this); pop the [object HTMLImageElement]

That is, the latter returns a html object (in this case a traverse HTML img object, so as HTMLImageElement). Many people, when using jquery, often this.attr ( 'src'); then it will error "Object does not support this property or method", which is why? In fact, understand the above example, we know what went wrong: the

Very simple, this operation is that HTML object, then, how HTML object will val () method, so that, in use, we can not directly use this method or property jquery to call directly.

 

Published 35 original articles · won praise 2 · Views 1361

Guess you like

Origin blog.csdn.net/weixin_41001497/article/details/105108836