jQuery not() 方法示例


jQuery 中的 not() 方法用於返回不匹配特定條件的元素。

語法

語法如下 −

$(selector).not(criteria,func(index))

其中,criteria 是一個選擇器表示式,而 func 是為該組中的每個元素執行的函式。

示例

現在,讓我們看一個實現 jQuery not() 方法的示例 −

 線上演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("button").click(function(){
         $("h2").not(".demo").css("color", "orange");
      });
   });
</script>
<style>
h2 {
   color: blue;
}
</style>
</head>
<body>
<h2>Student Info</h2>
<p>This is a demo text.</p>
<h2 class="demo">Exam Info</h2>
<p>This is a demo text.</p>
<h2 class="demo">Teacher's Info</h2>
<p>This is a demo text.</p>
<button>Click me</button>
</body>
</html>

輸出

這將產生以下輸出 −

現在,單擊“單擊我”以更新不符合特定條件的元素的文字顏色 −

更新時間:31-12-2019

189 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.