Created
July 14, 2015 17:19
-
-
Save tingwei628/1dfe0b452275fefef821 to your computer and use it in GitHub Desktop.
[JavaScript] 讀取Dom 並作排序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
目的: 對購物網站項目的價格做排序 (購物網站網址:http://activity.books.com.tw/ps/prog/2601/page/200/category/217234) | |
方法: 讀取Dom, 並sort() | |
*/ | |
var all = $('li'); //先取全部的li | |
var chose = []; | |
var price = []; | |
var title =[] | |
var bookstore = []; | |
for(var key in all){ | |
if(/li/.test(all[key].id)){ //檢查li id是否有值 | |
chose.push(all[key]); | |
} | |
} | |
for(var index =0 ; index<chose.length; index++){ | |
price[index] = parseInt(chose[index].getElementsByTagName('strong')[0].innerText); //個別價格 | |
title[index] = chose[index].getElementsByTagName('h5')[0].innerText; //個別名稱 | |
bookstore.push({title: title[index], price:price[index]}); | |
} | |
var sortbookstore = bookstore.sort(function(a,b){return a['price']-b['price']}); | |
console.log(sortbookstore); //排序過的 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment