简体   繁体   中英

You can wrap $() around an array of DOM elements, or around a jQuery object, but not around an array of jQuery objects

Not really sure how to phrase this question so it's generic enough. (and will rephrase, after I know what I'm asking). The problem, I believe deals with variables in a JQuery .find() function.

Problem: you can wrap $() around an array of DOM elements, or around a jQuery object, but not around an array of jQuery objects

The best I can do, at this time, is provide an example here

\n

The problem code in the previous fiddle, is here:

////////////////Neither of the following works////////////////
//nodelevel = nodesWithMinuses.find('div.node.level' + levelnumber);
  nodelevel = $(nodesWithMinuses).find('div.node.level' + levelnumber);
////////////////Neither of the previous works////////////////

Apparently you can wrap $() around an array of DOM elements, or around a jQuery object, but not around an array of jQuery objects.

Try changing this line:

var nodeWithMinus = thisplusminus.parent().parent();

to this:

var nodeWithMinus = thisplusminus.parent().parent()[0];

This will extract the DOM element and turn nodesWithMinuses into an array of DOM elements.

The variable nodesWithMinuses is an array of jQuery objects. You cannot apply jQuery method to anything else then a jQuery object. You have 2 options : either you declare nodesWithMinuses as a jQuery object and add objects to it via the add method :

var nodesWithMinuses = $();
//...
nodesWithMinuses.add(element);
// instead of nodesWithMinuses.push(element);

either find a way to convert the array to a jQuery object :

$($.map(nodesWithMinuses,function(el,i){return el.get(0)})).find('div.node.level' + levelnumber);

or traverse the array and search for the element in each object at a time :

var result = $();
$.each(nodesWithMinuses,function(i,el){
    result.add(nodesWithMinuses.find('div.node.level' + levelnumber));
});
console.log(result);

In your code, nodeWithMinuses is an array but you're trying to wrap it in the jQuery function. That doesn't work on arrays. Using a variable in jQuery's find function is fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM