ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## jQuery过滤方法 类过滤hasClass() ```javascript if($(this).hasClass("ClassName")){} ``` 下标过滤eq() ```javascript $().eq(n) // n是一个正整数,从0开始计算 $("li").eq(4) // $("li:eq(4)") ``` 判断过滤is() ```javascript $().is(":visible") //判断元素是否隐藏 $().is(":animated") //判断元素是否处在动画中(非常重要) $("input[type=checkbox]").is(":checked") //判断复选框是否被选中 $(this).is(":first-child") //判断是否第1个子元素 $().is(":contains('helicopter')") //判断文本中是否包含helicopter这个词 $().is(".red,.blue") //判断是否包含某些类名 快速地重复移入移出时,会出现一个动画累积的bug; is(":animated")这是用来判断当前所选元素是否处于动画状态 --------------------------------------------------------------------------- $(function () { $("#wrapper").hover(function () { if (!$(" #content", this).is(":animated")) { $(" #content", this).animate({ "bottom": "0px" }, 200); } }, function () { if (!$(" #content", this).is(":animated")) { $(" #content", this).animate({ "bottom": "-28px" }, 200); } }); }) ``` 反向过滤not() ```javascript $().not(expression) // $("ul li").not("#red").css("color", "red"); $("ul li:not(#red)").css("color", "red"); ``` 表达式过滤filter()和has() ```javascript filter(expression) $("ul li").filter("#red,#yellow").click(function () { $(this).css("color", "red"); }) ---------------------------------------------------------- filter(fn) $("ul li") .filter(function () {return $("span", this).length == 1;}) .css("color", "red"); $("span", this).length == 1 // 表示选择在内部子元素中span长度为1的li元素 ------------------------------------------------------------- $().has(expression) // filter()方法的缩小版, 没有函数过滤的方式 $("ul li").has("span").css("color", "red"); ```