jQuery的is()方法,jqueryis方法

來源:互聯網
上載者:User

jQuery的is()方法,jqueryis方法
is(expr|obj|ele|fn)概述

根據選取器、DOM元素或 jQuery 對象來檢測匹配元素集合,如果其中至少有一個元素符合這個給定的運算式就返回true。

如果沒有元素符合,或者運算式無效,都返回'false'。 '''注意:'''在jQuery 1.3中才對所有運算式提供了支援。在先前版本中,如果提供了複雜的運算式,比如層級選取器(比如 + , ~ 和 > ),始終會返回true

參數exprStringV1.0

字串值,包含供匹配當前元素集合的選取器運算式。

jQuery objectobjectV1.6

現有的jQuery對象,以匹配當前的元素。

elementExpressionV1.6

一個用於匹配元素的DOM元素。

function(index)FunctionV1.6

一個函數用來作為測試元素的集合。它接受一個參數index,這是元素在jQuery集合的索引。在函數, this指的是當前的DOM元素。

樣本參數expr 描述:

由於input元素的父元素是一個表單元素,所以返回true。

HTML 程式碼:
<form><input type="checkbox" /></form>
jQuery 代碼:
$("input[type='checkbox']").parent().is("form")
結果:
true
回呼函數 描述:

判斷點擊li標籤增加背景色為紅色,如果點擊的是第2個strong,當前的li增加背景色為綠色,

HTML 程式碼:
<ul>  <li><strong>list</strong> item 1 - one strong tag</li>  <li><strong>list</strong> item <strong>2</strong> - two <span>strong tags</span></li>  <li>list item 3</li></ul>
jQuery 代碼:
$("li").click(function() {  var $li = $(this),    isWithTwo = $li.is(function() {      return $('strong', this).length === 2;    });  if ( isWithTwo ) {    $li.css("background-color", "green");  } else {    $li.css("background-color", "red");  }});
結果:
       
  • list item 1 - one strong tag
  • list item 2 - two strong tags
  • list item 3
  • list item 4
  • list item 5

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.