最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to find elements without a class in Jquery? - Stack Overflow

programmeradmin4浏览0评论

I have the following html

<div class="one">One<div>
<div class="two">two<div>
<div >three<div>
<div >four<div>
<div class="three">five<div>

How would I find the div elements which don't have a class attribute? ie three and four?

I have the following html

<div class="one">One<div>
<div class="two">two<div>
<div >three<div>
<div >four<div>
<div class="three">five<div>

How would I find the div elements which don't have a class attribute? ie three and four?

Share Improve this question edited May 9, 2013 at 10:41 user2022859 asked May 9, 2013 at 10:32 SaaSaa 637 bronze badges 1
  • possible duplicate of jQuery get all divs which do not have class attribute – palaѕн Commented May 9, 2013 at 10:44
Add a ment  | 

6 Answers 6

Reset to default 10

You can use :not selector

$('div:not([class])');

here is API

And a simple Fiddle

Use :not selector to filter

$('div:not([class])');

Combine the :not() selector with the attribute present selector [class].

$("div:not([class])")

jsFiddle.

Another option is to use .not() with Has Attribute Selector

$('div').not('[class]')

There are different ways to do it. You could use .children() to get the list and then index into that. Or you could look up the second element and use .next() to get its sibling.

Assuming you're not wanting to select all the dividers which have no classes, you can use nth-child to select specific ones if you know exactly where they are within a container. This will select your class-less dividers:

$('div:nth-child(3)') // Selects the third divider
$('div:nth-child(4)') // Selects the fourth divider
$('div:nth-child(3), div:nth-child(4)') // Selects both

JSFiddle example.

Alternatively you can select using .prev() and .next():

$('div.two').next() // Selects the divider after div class="two"
$('div.three').prev() // Selects the divider before div class="three"
发布评论

评论列表(0)

  1. 暂无评论