Latest web development tutorials

jQuery is() 方法

jQuery HTML 方法 jQuery遍歷方法

實例

如果<p> 的父元素是<div> 元素,彈出提示信息:

if ($("p").parent().is("div")) {
alert("p 的父元素是div");
}

嘗試一下»

定義和使用

is() 方法用於查看選擇的元素是否匹配選擇器。


語法

$(selector).is( selectorElement, function (index , element) )

參數 描述
selectorElement 必須。 選擇器表達式,根據選擇器/元素/jQuery 對象檢查匹配元素集合,如果存在至少一個匹配元素,則返回true,否則返回false
function( index , element )

可選。 指定了選擇元素組要執行的函數。

  • index - 元素的索引位置
  • element - 當前元素( "this" 選擇器也可以使用)

jQuery HTML 方法 jQuery遍歷方法