Is the jQuery selector result a jQuery object or array or something else?

Asked 1 years ago, Updated 1 years ago, 38 views

·Is the jQuery selector result a jQuery object?
·Or array?
·Or an array-like but not strictly array-like object like DOM?

javascript jquery

2022-09-30 19:24

3 Answers

jQuery object.

It is also clear that you can specify a method for jQuery (e.g., $(".classname").css("display", "none") if the $(".classname") result is like an array or a DOM element list).


2022-09-30 19:24

I have a slight difference in perception, so I would like to add
You don't have to read it if it's just a conclusion.

Regarding the return value, I think it is a jQuery object.
However, it returned itself ($ inherited from the selector in the constructor).
So the actual function of css in $(".classname").css("display", "none") is
$.css and returns itself that inherited it, so you can run it.

Therefore, it is possible to inherit it by putting the return value in the variable. After inheritance, variables can run functions that can be called by jQuery objects.

// Just try substituting
varwork=$("#sample_data");

// If the return value is a jquery object, you can get HTML, right?
$("#res").append("["+work.html()+"]");
$("#res").append("<br/>");

// You can do it with DOM, right?
$("#res").append("["+work.innerHTML+"]");
$("#res").append("<br/>");

// By the way, what's your model?
$("#res").append("["+(typeofwork)+"]<br/>");



$("#res").append("<hr/>");
// What if I call an element that doesn't exist?
$("#res").append("["+(typeof$("#not_found")))+"]<br/>");



// Try inheriting
work = new$("#sample_data");

$("#res").append("<hr/>");
// Let's do the same thing as a substitute
$("#res").append("["+work.html()+"]");
$("#res").append("<br/>");
$("#res").append("["+work.innerHTML+"]");
$("#res").append("<br/>");
$("#res").append("["+(typeofwork)+"]<br/>";
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<divid="sample_data"><span>span_node</span><div>
<hr of >
<pre>
<code id="res">
</code>
</pre>


2022-09-30 19:24

Refer to the API documentation to find out.

Return value
jQuery
Extracted jQuery Objects

jQuery(expression, context) - jQuery Japanese Reference

jQuery(selector[,context])Returns:jQuery

http://api.jquery.com/jQuery/

Therefore, it is a jQuery object.

These questions will be understood in less than 30 seconds once you are able to examine them yourself.
It's more useful to know how to research than to know the conclusion.

Also, you should not write the code on ambiguous grounds such as $(".classname").css("display", "none"), so it feels like a JQuery object.APIs can change, different types of objects may be returned depending on the input, and different objects may exist with similar APIs.The only thing you can trust is the official API document.

Also, it is the last resort to confirm in the experiment.If it is not mentioned in the API document and the source code is too complicated to understand, we can't help it, so we will experiment and guess how it behaves.If you're lucky, you can move.If you're lucky.


2022-09-30 19:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.