DIV CSS 佈局教程網

jQuery 中 filter()、not() 與 split() 的使用方法
編輯:JQuery特效代碼     

filter() 與 not()

這兩個方法用來從一組相似元素中挑選或排除:

<script type="text/javascript">
$(document).ready(function() {
// 輸出 hello
alert($("p").filter(".selected").html());
// 輸出 How are you?
alert($("p").not(".selected").html());
});
</script>
<p class="selected">Hello</p>
<p>How are you?</p>

filter() 能把元素精簡到只剩下滿足條件的那些,not() 則移除所有滿足條件的元素。

split()

split() 是 JavaScript 原生字串方法,用來把字串依分隔符切成陣列,在處理格式化文字時非常好用:

<script type="text/javascript">
$(document).ready(function(){
$("input[@value=btn1]").click(function(){
// 以 ¥ 分割
alert($("span.sale").text().split("¥")[2]+"||"+$("span.sale").text().split("¥")[1]+"||"+$("span.sale").text().split("¥")[0]);
});
});
</script>
<span class="sale">
Out Sale: ¥160<br />
Deal Price: ¥120</span>

它的原理很簡單,例如把 msg = "2007/10/01" 用 msg.split("/") 切開,就會得到一個含三塊的陣列,接著就能輕鬆取出各段。上面的例子正是用 split("¥") 把價格文字拆開,再重組輸出。掌握 split,處理各類分隔格式的字串就游刃有餘了。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved