如何从jQuery包装器访问原始元素

brb*_*brb 16 javascript jquery wrapper

假设我有这个:

var wrap = $("#someId");
Run Code Online (Sandbox Code Playgroud)

我需要访问我会得到的原始对象

var orig = document.getElementById("someId");
Run Code Online (Sandbox Code Playgroud)

但我不想做document.getElementById.

有什么我可以wrap用来获得它吗?就像是:

var orig = wrap.original();
Run Code Online (Sandbox Code Playgroud)

我搜索得很高,但我找不到任何东西; 或许我不是在找正确的事.

lon*_*day 26

这个功能是get.您可以传递索引以get访问该索引处的元素,因此wrap.get(0)获取第一个元素(请注意,索引是基于0的,就像数组一样).您还可以使用负索引,因此wrap.get(-2)在选择中获取最后一个元素.

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
Run Code Online (Sandbox Code Playgroud)

您还可以使用类似数组的语法来访问元素,例如wrap[0].但是,您只能使用正索引.

wrap[0];      // get the first element
wrap[1];      // get the second element
wrap[6];      // get the seventh element
Run Code Online (Sandbox Code Playgroud)


Jac*_*lin 5

$("#someId") 将返回一个 jQuery 对象,因此要获取实际的 HTML 元素,您可以执行以下操作:

wrap[0]wrap.get(0)