如果没有分配类,我如何选择元素? [英] How can I select an element only if it doesn't have a class assigned?

查看:97
本文介绍了如果没有分配类,我如何选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改现有WordPress主题的CSS。该主题有许多特殊的样式的列表,附加到< li> 元素。因此,在< li> 元素上应用一个通用的 list-style:none / p>

我要更新CSS,以在< li>上重新创建 list-style ; 没有附加类的元素(即那些没有应用特殊样式的元素)。



我知道:not()伪选择器,并可能最终使用它来明确排除每个可能的类;但是,有十几个,这似乎是一个维护的噩梦。



有任何方式,使用CSS或jQuery,选择一个元素,通过指定它有没有类集?

解决方案

jQuery: $ class =]')(选择没有类属性的元素以及属性存在但是为空的元素)



CSS选择器 li [class =] 不幸的是工作方式不同。它只选择属性存在但是为空的元素(例如:< li class>< / li> < li class =>< / li>



$('li:not ])')解决方案可能不是你想要的(它不选择具有类属性存在但为空的元素)。



给定:

 < ul> 
< li> item 1< / li>
< li class> item 2< / li>
< li class => item 3< / li>
< li class =some-class> item 4< / li>
< / ul>

$('li [class =]')选择1,2,3

$('li [class =]')选择2,3

$('li:not([class])')只选择1个


I'm modifying the CSS of an existing WordPress theme. The theme has a lot of special styles for lists, attached to the <li> element. Because of this, there is a generic list-style:none rule applied to the <li> element.

I want to update the CSS to reinstitute the list-style defaults on <li> elements that don't have a class attached (i.e. those that don't have a special style applied). This needs to be selective to keep from ruining the fancy effects.

I am aware of the :not() pseudo-selector, and may end up using it to specifically exclude each possible class; however, there are a dozen of them and this seems like a maintenance nightmare.

Is there any way, either using CSS or jQuery, to select an element by specifying that it has no class set?

解决方案

jQuery: $('li[class=]') (selects both elements that have no class attribute as well as those for which the attribute is present, but empty)

The CSS selector li[class=""] unfortunately does not work the same way. It selects only those elements for which the attribute is present, but empty (ex: <li class></li> or <li class=""></li>)

The $('li:not([class])') solution might not be what you want (it doesn't select elements that have the class attribute present but empty).

Given:

<ul>
  <li>item 1</li>
  <li class>item 2</li>
  <li class="">item 3</li>
  <li class="some-class">item 4</li>
</ul>

$('li[class=]') selects 1,2,3
$('li[class=""]') selects 2,3
$('li:not([class])') selects just 1

这篇关于如果没有分配类,我如何选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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