9

Random just out of curiosity question:

Let's say for whatever reason I get an element back from a function

$(element)

But I want to remove the $( __ ) jQuery wrapper to leave the regular DOM Element:

element

Is this possible? (I'm sure it'd be smart to test $(element).length() to make sure it isn't more than 1 thing inside beforehand too...

jsFiddle

4

3 回答 3

21
var firstElem = $(element)[0];

or

var firstElem = $(element).get(0);

Calling get() without an index gives you an array of the elements.

Reference: jQuery get()

于 2012-09-28T14:46:16.130 回答
6

DOM elements are stored as properties at numeric zero-based indices, so you access them just like you would for any other object.

$jqObj[0];

Or get a full Array of elements using toArray()

$jqObj.toArray();
于 2012-09-28T14:46:39.687 回答
0

小提琴:http: //jsfiddle.net/xHj5d/2/

removeJWrapper($('#ohHeyo'));

function removeJWrapper (el) {
    console.log(el[0]);
}
于 2012-09-28T14:51:46.563 回答