jQuery 1.12.4是jQuery 1.x系列的最后一个正式稳定版本,由jQuery团队于2016年发布。该版本主要面向需要兼容旧版浏览器环境的网站和Web应用,尤其适用于仍需支持Internet Explorer 6、Internet Explorer 7、Internet Explorer 8等老旧浏览器的项目。
jquery中的is()方法可以判断元素是否可见,是否隐藏,是否选中。
下面我们分别举例介绍。
一、判断元素是否隐藏
如下html中的p元素是隐藏的:
nbsp;html>
<script></script>
<p>你看不到我</p>
<span></span>
<script javascript>
$('span').html('test p is visible? ' + $('#test').is(':visible'));
</script>
二、判断checkbox是否选中
jquery中可以通过xx.is(':checked')判断checkbox,radiobutton是否是选中状态,如下测试html
nbsp;html>
<script></script><p>
<input> chkChecked
<input> chkNoChecked
</p>
<span></span>
<script>
$('span').html('chkChecked checked? ' + $('input[name=chkChecked]').is(':checked') + '<br/> '
+'chkNoChecked checked? ' + $('input[name=chkNoChecked]').is(':checked') );
</script>
三、判断是否使用了某个样式
<meta>
<title>is函数介绍</title>
<style> //设置一些基本样式
p { width:60px; height:60px; margin:5px; float:left;
border:4px outset;
background
:green; text-align:center;
font-weight
:bolder; cursor:pointer; }
.blue { background:blue; } //样式1
.red { background:red; }//样式2
span { color:white; font-size:16px; }
p { color:red; font-weight:bolder; background:yellow;
margin:3px; clear:left; display:none; }
</style>
<script></script> //注意 这里使用的是jQuery官方的脚步库
<p></p> //注意这里出现了第一个p
<p></p>//注意这里出现了第2个p
<p></p>//注意这里出现了第3个p
<p></p>//注意这里出现了第4个p
<p><br><span>Peter</span></p>//注意这里出现了第5个p
<p></p>//注意这里出现了第6个p
<p> 脚本学堂,www.jbxue.com。</p>
<script>
$("p").one('click', function () { //$("p").one 代表对p元素附加一个事件,
//还能附加多个事件 譬如click或者mouseout时同时执行一些事情
if ($(this).is("
:first-child
")) { //is函数发挥作用了,is(":first-child") 代表
//判断此p是否第一个出现的p
$("p").text("It's the first p."); //text和html区别在于是否支持html标记
// 此时你在里面写个 alert是不会执行的
} else if ($(this).is(".blue,.red")) { //判断该p是否 具有blue或者red的 class
$("p").text("这是个蓝色或者红色的p");
} else if ($(this).is(":contains('Peter')")) { //判断p中是否存在Peter这个词
$("p").text("It's Peter!");
} else {
$("p").html("It's nothing <em>special.");
}
$("p").hide().slideDown("slow"); //这是一个动画效果。慢慢展现p的内容
$(this).css({"
border-style
": "inset", cursor:"default"});
});
</script>










