jquery的 filter()方法使用教程
定義和用法
filter() 方法返回符合一定條件的元素。
該方法讓您規(guī)定一個條件。不符合條件的元素將從選擇中移除,符合條件的元素將被返回。
該方法通常用于縮小在被選元素組合中搜索元素的范圍。
提示:filter() 方法是與 not() 方法相對的。
filter()是根據(jù)括號中的 屬性來進行篩選 比如:$(“p”).filter(“p”)
這樣是不允許的。filter括號里只一些屬性,或者包含什么的,不能是一個元素,例如”p”
//filter() --獲取具有某些屬性值的標簽 :(指定的對象集合進行過濾) $("li").filter(".item-1") //獲取class屬性值為item-1的li元素 $("li").filter(":even").css("background", "red"); //獲取所有偶數(shù)號的li元素,并將它們的背景顏色設(shè)為紅色 $("li").filter(".item-1,#abc").css("background-color", "yellow"); //獲取class屬性值為item-1的所有l(wèi)i標簽 ,同時獲取id屬性值為abc的所有l(wèi)i標簽 $("p").filter(".selected, :first");//獲取class屬性值為selected的所有P標簽,同時獲取第一個P標簽 //:contains() --獲取標簽里包含了某些文字的標簽 (contains是判斷文本的) //jQuery:has()和jQuery:contains()兩個方法比較類似。不同點在于:has是判斷標簽的 contains是判斷文本的 $("p p:contains('測試')").text(); //獲取p標簽下包含“測試”的p標簽的text值 //打印一下,結(jié)果為“測試1 測試2” //alert($("p").contains("測試").text()); //不能這么用,提示錯誤“contains不是一個函數(shù)”
html
<div class="model_content" style=""> <div class="model_content_search layui-bg-gray"> <form class="layui-form"> <div class="layui-form-item fl" style="margin-top: 15px;"> <label class="layui-form-label" style="width:66px;">選擇框</label> <div class="layui-input-inline" style="width:174px;"> <input id="search_val" class="layui-input" name="keyword" type="text" placeholder="請輸入關(guān)鍵字"> </div> </div> <div class="layui-input-inline fl" style="margin-top: 15px;"> <button class="layui-btn">搜索</button> </div> <div class="clear"></div> </form> </div> <div class="search_result"> <div class="mapAddress"> <ul> <li> <div class="addressInfo"> <h3>要素大市場</h3> <span class="bold">地址:</span>徽州大道與錦繡大道交叉口<br> </div> </li> <li> <div class="addressInfo"> <h3>錦繡大道</h3> <span class="bold">地址:</span>徽州大道與錦繡大道交叉口<br> </div> </li> <li> <div class="addressInfo"> <h3>蜀山區(qū)</h3> <span class="bold">地址:</span>徽州大道與錦繡大道交叉口<br> </div> </li> <li> <div class="addressInfo"> <h3>瑤海區(qū)</h3> <span class="bold">地址:</span>徽州大道與錦繡大道交叉口<br> </div> </li> </ul> </div> </div> </div> //input中輸入篩選 var $sub2 = $("#search_val"); $sub2.on("input", function () { var $iptVal2 = $sub2.val(); $(".mapAddress li").hide(); $(".mapAddress .addressInfo h3").filter(":contains('" + $iptVal2 + "')").parents("li").show(); });
總結(jié)
以上所述是小編給大家介紹的jquery的 filter()方法使用教程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- jQuery中each()、find()和filter()等節(jié)點操作方法詳解(推薦)
- jQuery里filter()函數(shù)與find()函數(shù)用法分析
- jquery中filter方法用法實例分析
- jQuery中filter()方法用法實例
- jQuery 過濾方法filter()選擇具有特殊屬性的元素
- jQuery filter函數(shù)使用方法
- 從jquery的過濾器.filter()方法想到的
- jQuery中filter()和find()的區(qū)別深入了解
- 基于jquery的has()方法以及與find()方法以及filter()方法的區(qū)別詳解
- jQuery中filter(),not(),split()使用方法
相關(guān)文章
jQuery實現(xiàn)跨域iframe接口方法調(diào)用
頁面a.html域名為www.a.com嵌入頁面http://www.b.com/b.html,b.html要調(diào)用a.html中的js函數(shù),由于兩個頁面不在一個域中,會提示沒權(quán)限。如何解決該問題呢,請看下面示例代碼。2015-03-03jQuery.Validate 使用筆記(jQuery Validation范例 )
學習jQuery Validation,于是手寫一公共范例,并收藏以便后用,里面附有測試代碼,需要的朋友一起來測試。2010-06-06