最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I access the original element from the jQuery wrapper - Stack Overflow

programmeradmin2浏览0评论

Assuming I have this:

var wrap = $("#someId");

I need to access the original object that I would get by

var orig = document.getElementById("someId");

But I don't want to do a document.getElementById.

Is there something I can use on wrap to get it? something like:

var orig = wrap.original();

I searched high and low but I didn't find anything; or maybe I'm not looking for the right thing.

Assuming I have this:

var wrap = $("#someId");

I need to access the original object that I would get by

var orig = document.getElementById("someId");

But I don't want to do a document.getElementById.

Is there something I can use on wrap to get it? something like:

var orig = wrap.original();

I searched high and low but I didn't find anything; or maybe I'm not looking for the right thing.

Share Improve this question asked Jul 8, 2011 at 10:35 brbbrb 2131 gold badge2 silver badges5 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 29

The function for this is get. You can pass an index to get to access the element at that index, so wrap.get(0) gets the first element (note that the index is 0-based, like an array). You can also use a negative index, so wrap.get(-2) gets the last-but-one element in the selection.

wrap.get(0);  // get the first element
wrap.get(1);  // get the second element
wrap.get(6);  // get the seventh element
wrap.get(-1); // get the last element
wrap.get(-4); // get the element four from the end

You can also use array-like syntax to access elements, e.g. wrap[0]. However, you can only use positive indexes for this.

wrap[0];      // get the first element
wrap[1];      // get the second element
wrap[6];      // get the seventh element

$("#someId") will return a jQuery object, so to get at the actual HTML element you can do:

wrap[0] or wrap.get(0).

You can use get() to retrieve the HTML element.

var orig = wrap.get(0);

However, if wrap consists of multiple elements, you will need to know the correct index which to use when you use the get() function.

You can just use var orig = wrap[0]; as far as I know, if there's more than one element. If there's just the one, you can just use wrap without $() around it.

You can use wrap still.. Wrap is the same as 'orig' would be in the above! :)

If you really want:

var orig = wrap;
发布评论

评论列表(0)

  1. 暂无评论