通过jQuery添加类,但仅当不存在时 [英] add class via jquery but only when not exists

查看:85
本文介绍了通过jQuery添加类,但仅当不存在时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个类(class ="bbox")添加到ul列表中,但前提是不存在任何类. 这是我到目前为止所拥有的.如何在ul标签中使用jquery进行检查?

I would like to add a class (class="bbox") to a ul-list but only if no class exists. This is what i have so far. How do I check with jquery if a class exists in the ul-tag?

$("ul").addClass("bbox");

推荐答案

如果要检查是否存在特定的类,则:

If you want to check if a specific class exists then:

 if(!$("ul").hasClass("class-name")){
    $("ul").addClass("bbox");
 }

如果要检查是否存在任何类:

If you want to check if any class exists:

if ($('ul').attr('class') == '') {
    $("ul").addClass("bbox");
}

这篇关于通过jQuery添加类,但仅当不存在时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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