2012/11/12 13:00:08作者:佚名來源:網絡
【實例名稱】
Ajax效果的字符串過濾
【實例描述】
在Microsoft提供的Ajax框架中,有個根據第一個字母選擇單詞的過濾實例。本例將學習如何使用正則實現這種效果。
【實例代碼】
<HTML><HEAD><TITLE>過濾字符實例-學無憂(www.yzddtk.cn)</title> <script type="text/javascript"> function filterlist(selectobj) { // 過濾對象 this.selectobj = selectobj;
//要過濾得字符-i表示忽略大小寫,""表示不忽略 this.flags = 'i'; // 要過濾的項-選擇是文本還是值 this.match_text = true; this.match_value = false; //調試參數 this.show_debug = false; //初始化的方法 this.init = function() { if (!this.selectobj) return this.debug('沒有實現過濾的控件'); if (!this.selectobj.options) return this.debug('過濾控件中沒有內容'); //制作選擇項的副本 this.optionscopy = new Array(); if (this.selectobj && this.selectobj.options) { for (var i=0; i < this.selectobj.options.length; i++) { // 創建一個新的選擇項對象 this.optionscopy[i] = new Option(); // 設置選擇項的文本 this.optionscopy[i].text = selectobj.options[i].text; // 設置選擇項的值 if (selectobj.options[i].value) { this.optionscopy[i].value = selectobj.options[i].value; } else { this.optionscopy[i].value = selectobj.options[i].text; } } } } this.reset = function() { //重置選擇項 this.set(''); }
//實現過濾的方法 this.set = function(pattern) { var loop=0, index=0, regexp, e; if (!this.selectobj) return this.debug('沒有實現過濾的控件'); if (!this.selectobj.options) return this.debug('過濾控件中沒有內容');
// 清空列表中的內容 this.selectobj.options.length = 0;
// 使用正則實現字符過濾-初始化正則 try { regexp = new RegExp(pattern, this.flags);
} catch(e) { if (typeof this.hook == 'function') { this.hook(); } return; } // 循環添加過濾后的結果 for (loop=0; loop < this.optionscopy.length; loop++) { // 定義選擇項 var option = this.optionscopy[loop]; // 實現正則式過濾 if ((this.match_text && regexp.test(option.text)) || (this.match_value && regexp.test(option.value))) { // 使用過濾結果創建新選擇項
this.selectobj.options[index++] = new Option(option.text, option.value, false); } } if (typeof this.hook == 'function') { this.hook(); } }
//設置正則表達式的過濾標志 this.set_ignore_case = function(value) { if (value) { this.flags = 'i'; } else { this.flags = ''; } } this.debug = function(msg) { //調試方法 if (this.show_debug) { alert('過濾結果: ' + msg); } } this.init(); //調用初始化方法 } </script> </HEAD> <BODY> <H1>過濾字符實例</H1> <FORM name=myform action=""><SELECT size=5 name=myselect> <OPTION>Aefdf Rsses<OPTION>Lbadmn Adjlf<OPTION>Monica Betty<OPTION>Daniel Jack<OPTION>Bill Gayes<OPTION>Lama Tampel<OPTION>Natty Lees<OPTION>Harry J. Leoo<OPTION>Matty McColm<OPTION>Carrie-Anne Moss<OPTION>Collin Chou<OPTION>Genevieve O'Reilly<OPTION>Harold Perrineau Jr.<OPTION>Jada Pinkett Smith<OPTION>Adrian Rayment<OPTION>Neil Rayment<OPTION>Bruce Spence<OPTION>Hugo Weaving<OPTION>Lambert Wilson<OPTION>Anthony Wong</OPTION></SELECT> <SCRIPT type=text/javascript> var myfilter = new filterlist(document.myform.myselect); //調用過濾的方法 </SCRIPT> <P>過濾: <A title="Clear the filter" href="javascript:myfilter.reset()">重置</A> <A title="Show items starting with A" href="javascript:myfilter.set('^A')">A</A> <A title="Show items starting with B" href="javascript:myfilter.set('^B')">B</A> <A title="Show items starting with C" href="javascript:myfilter.set('^C')">C</A> <A title="Show items starting with D" href="javascript:myfilter.set('^D')">D</A> <A title="Show items starting with E" href="javascript:myfilter.set('^E')">E</A> <A title="Show items starting with F" href="javascript:myfilter.set('^F')">F</A> <A title="Show items starting with G" href="javascript:myfilter.set('^G')">G</A> <A title="Show items starting with H" href="javascript:myfilter.set('^H')">H</A> <A title="Show items starting with I" href="javascript:myfilter.set('^I')">I</A> <A title="Show items starting with J" href="javascript:myfilter.set('^J')">J</A> <A title="Show items starting with K" href="javascript:myfilter.set('^K')">K</A> <A title="Show items starting with L" href="javascript:myfilter.set('^L')">L</A> <A title="Show items starting with M" href="javascript:myfilter.set('^M')">M</A> <A title="Show items starting with N" href="javascript:myfilter.set('^N')">N</A> <A title="Show items starting with O" href="javascript:myfilter.set('^O')">O</A> <A title="Show items starting with P" href="javascript:myfilter.set('^P')">P</A> <A title="Show items starting with Q" href="javascript:myfilter.set('^Q')">Q</A> <A title="Show items starting with R" href="javascript:myfilter.set('^R')">R</A> <A title="Show items starting with S" href="javascript:myfilter.set('^S')">S</A> <A title="Show items starting with T" href="javascript:myfilter.set('^T')">T</A> <A title="Show items starting with U" href="javascript:myfilter.set('^U')">U</A> <A title="Show items starting with V" href="javascript:myfilter.set('^V')">V</A> <A title="Show items starting with W" href="javascript:myfilter.set('^W')">W</A> <A title="Show items starting with X" href="javascript:myfilter.set('^X')">X</A> <A title="Show items starting with Y" href="javascript:myfilter.set('^Y')">Y</A> <A title="Show items starting with Z" href="javascript:myfilter.set('^Z')">Z</A> </P></FORM></BODY></HTML>
【運行效果】
【難點剖析】
本例的重點是使用正則表達式實現字符的過濾,還實現了動態添加列表項的功能。代碼中使用“new”關鍵字創建了RegExp對象,其用來提供簡單的正則表達式支持功能。使用RegExp對象的“test”方法,可以對指定的字符串執行一個正則表達式搜索,并返回一個布爾值指示是否找到匹配的模式。當搜索到結果時就使用“new Option”動態創建一個列表項,同時添加到列表中。
【源碼下載】
為了JS代碼的準確性,請點擊:Ajax效果的字符串過濾 進行本實例源碼下載
標簽: 字符串
相關文章
ip雷達 v5.0.2綠色版
詳情WinSCPv5.15.1綠色中文版
詳情filezilla client v5.5
詳情wireshark中文版 v3.4.9
詳情Boson Netsimv10.8破解版
詳情風行播放器 v3.0.6.106
詳情愛奇藝客戶端 v10.1.0.6636官方版
詳情搜狐影音 v7.0.19.0官方版
詳情樂視視頻 v7.3.2.192
詳情華數tv客戶端PC版 v1.1.2.0
詳情cbox央視影音 v5.1.2.1經典版
詳情軟媒收音機(魔方) v1.13
詳情傾聽網絡收音機 v3.0.0.0
詳情魅影流聲全球網絡收音機 v1.0.0.1
詳情酷狗電臺 v8.2.75
詳情暴龍網絡收音機 v2.5.1
詳情克莉2v1.0中文版
詳情冬日計劃v1.2中文版
詳情刺客信條英靈殿v1.0吾愛破解版
詳情刺客信條英靈殿終極v1.0免安裝破解版
詳情動物森友會v1.10.0最新破解版
詳情哈迪斯殺出地獄v1.37中文破解版
詳情嗜血印中文豪華版v1.0豪華版破解版 附游戲攻略秘籍
詳情城市戰斗v1.0中文破解版
詳情尼爾人工生命v1.0steam免費破解版
詳情尼爾人工生命升級版v1.0PC破解版
詳情尼爾人工生命升級版v1.2247中文破解版
詳情層層恐懼2中文破解版v1.0綠色免安裝版
詳情往日不再v1.0 steam破解版
詳情往日不再v1.0pc破解版
詳情救贖之路v1.0中文破解版
詳情斯蓋爾女仆v1.0中文破解版
詳情新盜墓筆記官方版v1.215.589730
詳情無盡的拉格朗日無限金幣鉆石版v1.2.267172破解版
詳情明日之后無限小太陽版v1.0.246安卓版
詳情雙生幻想vivo版v6.6.83安卓版
詳情劍網1歸來內購免費版v1.1.116安卓版
詳情模擬城市我是市長國際版v0.69.21342.22832安卓版
詳情迷你世界999999迷你幣版v1.20.5破解版
詳情王牌戰爭無限內購版v9.7安卓版
詳情云上城之歌不氪金版v10.16安卓版
詳情王鏟鏟的致富之路破解版v1.1.8安卓版
詳情創造與魔法無限點券版v1.0.0505安卓版
詳情狩獵使命九游版v1.6.6.000
詳情魔鏡物語變態版v2.1.4無限金幣版
詳情黑月手游免費版v2.2.0安卓版
詳情曙光英雄v1.0.12.0.20無限鉆石版
詳情原始傳奇無限元寶版v1.6.806內購破解版
詳情