Home  >  Article  >  Web Front-end  >  JQuery Confusion—Wrapper Set DOM Node_jquery

JQuery Confusion—Wrapper Set DOM Node_jquery

WBOY
WBOYOriginal
2016-05-16 18:44:27895browse

$('#someElement') and $('#someElement')[0], this statement caused me confusion for a while. At the beginning, every time I used it, I always replaced one with the other when an error occurred.
Although the function can be completed every time, it is always unspeakably frustrating! Later, I finally understood the reason: although $('#someElement') only selects elements, it is still a packaging set, and
cannot use DOM node methods (such as SetAttribute, etc.). And $('#someElement')[0] does return the first element in the packaging set, all methods it can use DOM nodes.

In short: When using JQuery, you need to pay attention to the difference between wrapper sets and DOM nodes. The methods provided by JQuery are all wrapper set methods. You cannot use JQuery methods on DOM nodes.
Similarly, DOM nodes Methods also cannot be used on wrapper sets, but can only be applied to DOM nodes. Of course, in addition to methods, there are also attributes (such as innerHTML).
At the same time, I can also wrap DOM nodes into wrapper sets: For example, we often use $(this) to wrap the nodes into wrapper sets and then use JQuery methods to process them.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn