admin管理员组文章数量:1287973
I have a doubt about the jQuery core. From the docs and a couple of books I get:
var obj = $("div");
Which returns a wrapper object AKA a collection of selected DOM elements with additional methods (correct me if I'm wrong). I've read the jQuery function $()
returns a wrapper object, or does it really return a copy of jQuery plus a collection of wrapped elements?
I have a doubt about the jQuery core. From the docs and a couple of books I get:
var obj = $("div");
Which returns a wrapper object AKA a collection of selected DOM elements with additional methods (correct me if I'm wrong). I've read the jQuery function $()
returns a wrapper object, or does it really return a copy of jQuery plus a collection of wrapped elements?
2 Answers
Reset to default 6It returns an instance of a jQuery object, wrapping the elements you selected with your CSS selector (in this case, a jQuery object wrapping all the divs in the document).
jQuery isn't something that is "copied" - it is behavior that is wrapped around elements in the DOM.
var jqDivs = $("div");
var jqButtons = $("button");
var jqSubmitButton = $("button#submit");
Those 3 vars reference 3 different objects. All of them implement the same jQuery behaviors, but they do it upon different elements. jqDivs.hide() would hide the divs - jqButtons.hide() would hide the buttons, and jqSubmitButton.hide() would hide only the button with id=submit.
The term "wrap" is a bit misleading to me. jQuery keeps references to elements matched by the supplied selector as numeric properties of the jQuery instance returned by the call, so:
var allTheDivs = $('div');
returns a jQuery object with references to all the divs in the document, and:
allTheDivs[0]; // or allTheDivs['0'];
is a reference to the first one. So if you then do:
allTheDivs.hide();
it calls the hide method of the jQuery instance allTheDivs which cycles over all the referenced elements and hides them. But don't try:
allTheDivs['0'].hide()
as that will try to call hide on a DOM element, which probably won't exist so will restult in an error.
本文标签: javascriptClarification of jQuery quotwrapperquot objectStack Overflow
版权声明:本文标题:javascript - Clarification of jQuery "wrapper" object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741330513a2372739.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论