admin管理员组

文章数量:1315978

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

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////////////////

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

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////////////////
Share Improve this question edited Oct 17, 2012 at 9:51 hippietrail 17k21 gold badges109 silver badges179 bronze badges asked Nov 17, 2011 at 20:19 Ryan SchultzRyan Schultz 3374 silver badges19 bronze badges 3
  • 1 What problem? If you don't know what you're asking, don't ask. – user395760 Commented Nov 17, 2011 at 20:21
  • 1 Before I let guests visit my house, I tidy it up a bit to make sure it looks reasonably nice. Why do people think that it should be any different for their code? – zzzzBov Commented Nov 17, 2011 at 20:22
  • @zzzzBov LOL I've been following you for the past 25 minutes and almost all of your ments have been hostile to condescending. I'd flag them if they weren't so funny. – puk Commented Nov 17, 2011 at 20:30
Add a ment  | 

3 Answers 3

Reset to default 6

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.

本文标签: