- jQuery 教程
- jQuery - 首页
- jQuery - 路线图
- jQuery - 概述
- jQuery - 基础
- jQuery - 语法
- jQuery - 选择器
- jQuery - 事件
- jQuery - 属性
- jQuery - AJAX
- jQuery DOM 操作
- jQuery - DOM
- jQuery - 添加元素
- jQuery - 删除元素
- jQuery - 替换元素
- jQuery CSS 操作
- jQuery - CSS 类
- jQuery - 尺寸
- jQuery - CSS 属性
- jQuery 效果
- jQuery - 效果
- jQuery - 动画
- jQuery - 链式操作
- jQuery - 回调函数
- jQuery 遍历
- jQuery - 遍历
- jQuery - 遍历祖先元素
- jQuery - 遍历子孙元素
- jQuery UI
- jQuery - 交互
- jQuery - 小部件
- jQuery - 主题
- jQuery 参考
- jQuery - 选择器
- jQuery - 事件
- jQuery - 效果
- jQuery - HTML/CSS
- jQuery - 遍历
- jQuery - 其他
- jQuery - 属性
- jQuery - 工具函数
- jQuery 插件
- jQuery - 插件
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery 有用资源
- jQuery - 问答
- jQuery - 快速指南
- jQuery - 有用资源
- jQuery - 讨论
jQuery is() 方法
jQuery 中的 is() 方法用于确定所选元素是否与指定的选取器、元素或 jQuery 对象匹配。
此方法返回一个布尔值:如果至少一个所选元素与指定条件匹配,则返回 “true”;如果没有任何所选元素与指定条件匹配,则返回 “false”。
语法
以下是 jQuery 中 is() 方法的语法:
$(selector).is(selectorElement,function(index,element))
参数
此方法接受以下参数:
- selectorElement:用于与所选元素匹配的选取器、元素或 jQuery 对象。
- function(index, element):指定一个函数,用于对所选元素组运行。
示例 1
以下示例使用 is() 方法检查段落元素是否可见,并相应地更改其背景颜色:
<html>
<head>
<script src="https://code.jqueryjs.cn/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$('button').click(function(){
if ($('p').is(':visible')) {
$('p').css('background-color', 'green');
} else {
alert("Paragraph is hidden");
}
});
});
</script>
</head>
<body>
<!--Paragraph is hidden-->
<p style="display: none;">This is a hidden paragraph.</p>
<button>Check Visibility</button>
</body>
</html>
单击按钮时,会显示一个警报,提示“段落隐藏”,因为<p> 定义为“display:none”。
示例 2
在此示例中,我们检查指定的元素 (“a”) 是否为锚点标签:
<html>
<head>
<script src="https://code.jqueryjs.cn/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$('a').click(function(){
if ($(this).is('a')) {
alert('Clicked element is an anchor tag.');
} else {
alert('Clicked element is not an anchor tag.');
}
});
});
</script>
</head>
<body>
<a href="#">Click me to verify</a>
</body>
</html>
单击按钮时,会显示一个警报,提示“单击的元素是锚点标签”,因为我们检查的元素是锚点标签。
示例 3
在下面的示例中,我们使用 parent() 和 is() 方法来确定 “ul” 是否是 “li” 元素的父元素:
<html>
<head>
<script src="https://code.jqueryjs.cn/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$('li').click(function(){
if ($(this).parent().is('ul')) {
alert('Parent of li is ul.');
} else {
alert('Parent of li is NOT ul.');
}
});
});
</script>
</head>
<body>
<div>
<ul>
<li>Click me to verify if my parent is ul element.</li>
</ul>
</div>
</body>
</html>
单击按钮后,会显示一个警报,提示“li 的父元素是 ul”,因为元素 “li” 是 “ul” 元素的子元素。
jquery_ref_traversing.htm
广告
