jQuery Select Element that has no Class or ID
6799 просмотра
3 ответа
Trying to find a selector to get all elements that have both no class, and no id set to them.
So far I have 2 different outputs depending on if there is a space in the selector:
// outputs
var noID = $('*:not([id])');// 144 - may have a class
var noClass = $('*:not([class])'); // 100 - may have an id
var withSpace = $('*:not([id]) *:not([class])'); // 99 ?
var noSpace= $('*:not([id])*:not([class])'); // 84 ?
Which one is correct, my guess is the noSpace
- but I don't know. Anyone tried this before?
My guess is that with the space, the selector is going inside the tag that has no ID, and selecting the children elements that have no class associated with them.
And the noSpace
result is the correct one, as it selects only the elemets that have both no class, and no id.
Can someone verify? Thanks!
Answer
Use this selector to find elements that have neither a class, or an id associated with them.
$('*:not([id]):not([class])');
bonus: $('body *:not([id]):not([class])');
- If you only want to deal with the actual content
Ответы (3)
4 плюса
withSpace - $('*:not([id]) *:not([class])');
will find all elements with no class that are inside an element without an ID. Putting a space in the selector is like calling find seperately.
You could change noSpace to be this instead and still get the right result:
var noSpace= $('*:not([id]):not([class])'); // second * not needed
Автор: Richard Dalton
Размещён: 28.05.2014 03:22
3 плюса
$("*").not("[class],[id]")
You can keep adding attributes or classes or tags by a comma seperator
Additional notes http://api.jquery.com/not-selector/
The .not() method will end up providing you with more readable selections than pushing complex selectors or variables into a :not() selector filter. In most cases, it is a better choice
(:not) is faster since it is css3 selector browser but show a real slow query in older browsers ie8 or the browsers that dont support css3
http://jsperf.com/jquery-css3-not-vs-not test results
Автор: niko Размещён: 28.05.2014 03:281 плюс
$('*:not([id]):not([class])')
Your withSpace
will select elements that don't have a class and have a parent which don't have an id. Not sure about noSpace
. UPDATE Oh, it's does the same thing as mine, actually. So, the answer is your last selector.
Вопросы из категории :
- javascript Как определить, какой из указанных шрифтов был использован на веб-странице?
- javascript Валидация клиентской стороны ASP.Net
- javascript Длина объекта JavaScript
- javascript Получение текста из выпадающего списка
- javascript Скрипт входа со скрытыми кнопками
- javascript Как автоматически изменить размер текстовой области с помощью Prototype?
- jquery Прокрутка переполненных DIV с помощью JavaScript
- jquery Экранирование строк HTML с помощью jQuery
- jquery Как сравнить HTML-сущность с JQuery
- jquery Есть ли функция "существует" для jQuery?
- jquery Как удалить все параметры в окне выбора, а затем добавить один вариант и выбрать его с помощью jQuery?
- jquery Получение идентификатора элемента, который вызвал событие
- jquery-selectors Существует ли регистр символов jQuery: содержит селектор?
- jquery-selectors Регулярные выражения селектора jQuery
- jquery-selectors How do you check if a selector matches something in jQuery?
- jquery-selectors Как получить детей из селектора $ (this)?
- jquery-selectors Выбор нескольких классов с помощью jQuery
- jquery-selectors Как узнать, какой переключатель выбран с помощью jQuery?