jQuery遍曆not的用法

來源:互聯網
上載者:User

標籤:表示   tor   query   讀取   詳細   版本   not   element   log   

從包含所有段落的集合中刪除 id 為 "selected" 的段落:

$("p").not("#selected")
定義和用法

not() 從匹配元素集合中刪除元素。

文法 1
.not(selector)
參數 描述
selector 字串值,包含用於匹配元素的選取器運算式。
文法 2
.not(element)
參數 描述
element 一個或多個需要從匹配集中刪除的 DOM 元素。
文法 3
.not(function(index))
參數 描述
function(index) 用於檢測集合中每個元素的函數。this 是當前 DOM 元素。
詳細說明

如果給定一個表示 DOM 元素集合的 jQuery 對象,.not() 方法會用匹配元素的子集構造一個新的 jQuery 對象。所應用的選取器會檢測每個元素;不匹配該選取器的元素會被包含在結果中。

請思考下面這個帶有簡單列表的頁面:

<ul>  <li>list item 1</li>  <li>list item 2</li>  <li>list item 3</li>  <li>list item 4</li>  <li>list item 5</li></ul>

我們可以向清單項目集應用該方法:

$(‘li‘).not(‘:even‘).css(‘background-color‘, ‘red‘);
移除具體的元素

.not() 方法的第二個版本允許我們從匹配集中刪除元素,假設我們之前已經通過其他手段找到了這些元素。例如,設想一個列表已經將 id 應用到其中一個項目中:

<ul>  <li>list item 1</li>  <li>list item 2</li>  <li id="notli">list item 3</li>  <li>list item 4</li>  <li>list item 5</li></ul>

我們可以使用原生的 JavaScript 函數 getElementById() 讀取第三個清單項目,然後把它從 jQuery 對象中刪除:

$(‘li‘).not(document.getElementById(‘notli‘)).css(‘background-color‘, ‘red‘);

jQuery遍曆not的用法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.