我有多个类名为“level1”的对象(这是基于 asp.net 构建的,该元素不允许在此处设置 ID)
我想做的是获取其中之一并分配 css 属性
$(".level1")[1].css({"background-color":"yellow"});
我得到了错误
Error: Object doesn't support property or method 'css'
当我这样做时:
console.log($(".level1")[1].innerHTML);
我得到了正确的 innerHTML,所以我知道它得到了正确的元素
最佳答案
使用eq
:
$(".level1").eq(1).css({
"background-color": "yellow"
});
$(".level1")[1]
返回 HTML
而不是 jQuery Object
,因此您不能调用 jQuery 方法它。请改用 eq
。
eq(1)
将获得 Second 类 level1
的元素(索引从 0 开始)
Reduce the set of matched elements to the one at the specified index.
关于javascript - 错误 : Object doesn't support property or method 'css' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30758922/