如何在所有没有JS或JQ中的类的表上添加类? [英] how to add class to all tables without class in JS or JQ?

查看:50
本文介绍了如何在所有没有JS或JQ中的类的表上添加类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将JS或JQ中的类添加到站点中所有没有类的表中,有些已经作为类,因此我需要将其仅添加到没有人的表中...请咨询? 我还需要班级唯一,班级名称后面必须有数字

i need to add class in JS or JQ to all the tables without class in a site, some already as class so i need it to add the class only to the tables who doesn't have... please advice? edit: i also need the class to be unique with number after the name of the class

$('table').each(function(i){
                $(this).addClass('classname',i);
            });

推荐答案

如果您只需要向没有任何类的表(而不只是所有表)添加类,则可以使用:

If you need to add a class to only those tables without any classes (and not just all your tables), you can use:

$('table').each(function() {
  if (!$(this).attr('class') || !$(this).attr('class').length) $(this).addClass('zzz')
})

这篇关于如何在所有没有JS或JQ中的类的表上添加类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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