今天才發現原來篩選標簽還可以這么用。
not(expr|ele | fn):從匹配元素的集合中刪除與指定表達式匹配的元素
下面demo中的使用: var $category = $(".sub-category-box>ul>li:gt(2):not(:last)"); //列表中索引大于2的,除了最后一個
filter(expr|obj|ele|fn) :r篩選出與指定表達式匹配的元素集合。這個方法用于縮小匹配的范圍。用逗號分隔多個表達式
$("ul>li").filter(":contains('佳能'),:contains('索尼'),:contains('三星')") .toggleClass("promoted");// 篩選出li標簽中包含佳能、索尼、三星的標簽,并設置class
找個demo中使用了這兩個方法。 突然感覺 jQuery真的是好強大。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; padding: 0; } .sub-category-box{ width: 300px; border: 1px solid #000; margin: 20px auto; background-color: gainsboro; } .sub-category-box ul{ list-style: none; width: 100%; overflow: hidden; } .sub-category-box ul li{ float: left; width: 95px; height: 35px; text-align: center; background-color: darkorange; box-sizing: border-box; line-height: 40px; border-radius: 5px; margin: 2px; } .promoted{ background-color: red !important; color: white !important; } .sub-category-box .show-more{ width: 100%; height: 30px; border: 1px solid #000; text-align:center; } .sub-category-box .show-more a{ text-decoration: none; line-height: 30px; } </style> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script> $(function () { //列表中索引大于2的,除了最后一個 var $category = $(".sub-category-box>ul>li:gt(2):not(:last)"); $category.hide(); $('.show-more').click(function () { $category.stop().slideToggle(300); //篩選出符合條件的選擇器 $("ul>li").filter(":contains('佳能'),:contains('索尼'),:contains('三星')") .toggleClass("promoted"); return false; }); }); </script></head><body><div class="sub-category-box"> <ul> <li>佳能</li> <li>索尼</li> <li>三星</li> <li>尼康</li> <li>松下</li> <li>卡西歐</li> <li>富士</li> <li>柯達</li> <li>理光</li> <li>明基</li> <li>松下</li> <li>卡西歐</li> <li>富士</li> <li>柯達</li> <li>海爾</li> <li>其他品牌</li> </ul><div class="show-more"> <a href="javasript:void(0);">顯示全部品牌</a></div></div></body></html>以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!
新聞熱點
疑難解答