简体   繁体   English

如何检查HTML元素是否没有类

[英]How to check if an HTML element has no class

I want to append a class to an anchor tag, which as no class. 我想将一个类附加到一个锚标记,它不是一个类。 Also this anchor tag is inside many DIV elements and h4 tag So h4 has a class say abc inside this h4 tag there is this anchor tag which doesn't have any class.This is what I tried but not working. 此锚标签也在许多DIV元素和h4标签内。所以h4在这个h4标签中有一个类说abc,这个锚标签没有任何类。这是我尝试但不起作用的。 I want to append a class to the anchor that doesn't have a class at present. 我想将一个类附加到目前没有类的锚点。

$(document).ready(function(){
 $('.abc a').hasClass(''){
   $(this).addClass('active');
 }
});

FIDDLE 小提琴

你可以使用:

$('.abc a').filter(':not([class])').addClass('active');

Another Solution :but @A. 另一种解决方案 :但是@A。 Wolff is better : 沃尔夫更好:

$(document).ready(function(){
   alert($('.abc a').attr('class'))
    if(!$('.abc a').attr('class')){
     $('.abc a').addClass('active');
    }
    alert($('.abc a').attr('class'))
});

You can get the length of the attribute class. 您可以获取属性类的长度。
If it is 0, then you can add a class 如果为0,则可以添加一个类

$(document).ready(function () {
    var classes = $('.abc a').attr('class');
    var cLength = classes.trim().length;
    if (clength==0) {
        $('.abc a').addClass('active');
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM