2

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");

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

or / and

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

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

used info from http://api.jquery.com/get/

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

4

1 回答 1

7

当你从一个带有下标的集合中访问一个项目时[i],你实际上是从 jQuery 对象中解包它,并访问一个原始 DOM 节点,它没有像addClass和这样的方法css

改用.eq()

var i = 0;
$(".question").eq(i).addClass("show");
$(".question").eq(i).css("display", "block");
于 2013-07-24T20:18:07.163 回答