请看下面的问答,第二个答案(有很多赞) Jquery: How to check if the element has certain css class/style
看起来像这样:
if ($('#yourElement').css('position') == 'absolute') {
// true
}
这太棒了,但我想要类似的东西
if ($('#yourElement').css('position') == 'absolute') {
$(this).addClass('xxx');
}
因此 ID 为“yourElement”的元素添加了一个类
最简单的方法是什么
最佳答案
你可以这样做:
$('#yourElement').addClass(function(index, currentClass) {
return $(this).css('position') == 'absolute' ? 'xxx' : '';
});
有关语法的更多信息,请查看 API for .addClass()
.
关于javascript - 如果 css 有样式,jquery addClass 到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15201745/