JQuery 取得物件的HTML:
JQuery 讀取JSON文件並進行資料搜尋
**JS**
//search function
//1. load external json file
//2. search json data
//3. output result
$.getJSON("model.json", function(data) {
var items = [];
var search = {};
var model = "";
var productPath = "../exec/";
var imgPath = "../proimages/pb/";
//搜尋
$.each(data, function(keym, valm) {
$.each(valm, function(key, val) {
if (urlParams['catid'] == "all") {
if (urlParams['type'] == key) {
if (val.search(urlParams['kwd2']) != -1) {
search[keym] = valm;
//console.log(search);
}
}
}
})
});
//console.log(search);
//印出
$.each(search, function(key, val) {
model = "
items.push(model);
});
$("
});
**HTML**
關於抓取img資料時透過load會有cache的問題,導致取得的資料不正確,此時透過下面的做法可以解決
$(document).ready(function() {
var newImage = "images/002.jpg"; //Image name
$("a.tnClick").click(function() {
$("#myImage").hide() //Hide it
.one('load', function() { //Set something to run when it finishes loading
$(this).fadeIn(); //Fade it in when loaded
})
.attr('src', newImage) //Set the source so it begins fetching
.each(function() {
//Cache fix for browsers that don't trigger .load()
if(this.complete) $(this).trigger('load');
});
});
})
Array做排序的方式