Javascript jQuery从索引问题中选择项
我已从列表中选择了li项,但如果我尝试对其使用函数,则会出现以下错误:Javascript jQuery从索引问题中选择项,javascript,jquery,Javascript,Jquery,我已从列表中选择了li项,但如果我尝试对其使用函数,则会出现以下错误: Uncaught TypeError: undefined is not a function var li = $( "li" ); console.log(li[elementToGet].offset());<----ERROR 正确打印所选项目您需要在jQuery对象上调用offset(),但您使用的索引器为您提供了DOM对象。您需要使用jQuery对象集合在特定索引处获取jQuery对象 console
Uncaught TypeError: undefined is not a function
var li = $( "li" );
console.log(li[elementToGet].offset());<----ERROR
正确打印所选项目您需要在jQuery对象上调用
offset()
,但您使用的索引器为您提供了DOM
对象。您需要使用jQuery对象集合在特定索引处获取jQuery对象
console.log(li.eq(elementToGet).offset());
试试看
$(li[elementToGet]).offset()
li[elementToGet]
返回纯JavaScript元素。您必须用jQuery重新装饰它。假设您想得到第一个li项偏移量
console.log(li.eq(0).offset());
console.log(li.eq(0).offset());