使用jquery时出现控制台错误-Uncaught TypeError:Object#< HTMLElement>没有办法 [英] console error when using jquery - Uncaught TypeError: Object #<HTMLElement> has no method

查看:104
本文介绍了使用jquery时出现控制台错误-Uncaught TypeError:Object#< HTMLElement>没有办法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下js添加类或CSS样式,但出现控制台错误

I'm trying to add a class or a css style using the following js but getting a console error

var i = 0;

$(".question")[i].addClass("show");

获取以下控制台日志错误:未捕获的TypeError:对象#没有方法'addClass'

get the following console log error: Uncaught TypeError: Object # has no method 'addClass'

或/和

$(".question")[i].css("display", "block");

获取以下控制台日志错误:未捕获的TypeError:对象#没有方法'css'

get the following console log error: Uncaught TypeError: Object # has no method 'css'

来自 http://api.jquery.com/get/的信息

编辑
如果删除变量i并使用数字0或1,仍然无法正常工作

EDIT
Still doesn't working if get rid of the variable i, and use numbers 0 or 1

推荐答案

当您使用诸如[i]之类的下标访问集合中的项目时,实际上是从jQuery对象中将其展开,并访问了原始DOM节点,没有像addClasscss这样的方法.

When you access an item from a collection with a subscript like [i], you're actually unwrapping it from the jQuery object, and accessing a raw DOM node, which doesn't have methods like addClass and css.

改为使用 .eq() :

var i = 0;
$(".question").eq(i).addClass("show");
$(".question").eq(i).css("display", "block");

这篇关于使用jquery时出现控制台错误-Uncaught TypeError:Object#< HTMLElement>没有办法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆