Spec-Zone.ru › jQuery

Селектор атрибута [name]

Селектор attributeHas

Описание: Выбирает элементы, у которых указанный атрибут имеет любое значение.

  • версия добавления: 1.0jQuery( "[attribute]" )

    атрибут: Имя атрибута.

Пример:

Привяжите одиночный клик к div с id, который добавляет id в текст div.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>attributeHas demo</title>
  <script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
 
<div>no id</div>
<div id="hey">with id</div>
<div id="there">has an id</div>
<div>nope</div>
 
<script>
// Using .one() so the handler is executed at most once
// per element per event type
$( "div[id]" ).one( "click", function() {
  var idString = $( this ).text() + " = " + $( this ).attr( "id" );
  $( this ).text( idString );
});
</script>
 
</body>
</html>

Демо:

© The jQuery Foundation and other contributors
Licensed under the MIT License.
https://api.jquery.com/has-attribute-selector

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API