How can I select an item only if it does not have an assigned class?

I am modifying the CSS of an existing WordPress theme. A theme contains many special styles for lists attached to an element <li>. Because of this, there is a rule list-style:nonethat applies to an element <li>.

I want to update CSS to restore list-styledefault values ​​on elements <li>that don't have an associated class (i.e. those that don't have a special style). This should be selective so as not to destroy the bizarre effects.

I know the pseudo-selector: not () and can use it to specifically exclude every possible class; however, there are a dozen of them, and this seems like a nightmare to service.

Is there a way, using CSS or jQuery, to select an element, indicating that it does not have a set of classes?

+5
source share
3 answers

jQuery: $('li[class=]')(selects both elements that do not have a 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. It selects only those elements for which the attribute is present but empty (ex: <li class></li>or <li class=""></li>)

The solution $('li:not([class])')may not be what you want (it does not display elements that have a class attribute but are 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 only 1

+2
source

$('li:not([class])'); , .

Dan, , , , li class. , <li class> <li class="">...

$('li:not([class])');
$('li[class=]');
$('li[class=""]');
+8

Interestingly, you can do this:

$('li[class=]')

or

$('li:not([class]=)')

TO

0
source

All Articles