當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JQuery 是否有屬性[name]用法及代碼示例

用法
attributeHas selector

說明:選擇具有指定屬性的元素,具有任何值。

  • 添加的版本:1.0jQuery( "[attribute]" )

    attribute: 一個屬性名稱。

例子:

將單擊綁定到具有 id 的 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.5.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>

演示:

相關用法


注:本文由純淨天空篩選整理自jquery.com大神的英文原創作品 是否有屬性[name]。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。