DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> jquery+json實現的搜索加分頁效果
jquery+json實現的搜索加分頁效果
編輯:JQuery特效代碼     
1.Web 裡面有三個目錄:
  1.1 content.htm    //這個文件直接運行就行
  1.2 js          //這裡面有用到的幾個js 文件,其中 以db 開頭的就是數據源(就是json 串)
  1.3 css         //css 就不用說了
2. 直接運行 content.htm 就可以直接打開查看分頁和搜索效果了
3. 原理很簡單,只是用了javascript 的index 函數
核心代碼:
代碼如下:
/*
author: liulf
function:
var __db_cpcj=vCpcj;
var __db_cp=vCp;
var vCp={"cppage":[
{"cpdh":"A01000101","page":"1","ca":"1309-36-0","mc":"硫鐵礦","ename":"Pyrite","bm":"黃鐵礦"},
{"cpdh":"A01000201","page":"1","ca":"1309-36-0","mc":"硫精砂","ename":"Pyrite concentrate;Sulfur concentrate","bm":""},
{"cpdh":"A01000202","page":"1","ca":"1309-36-0","mc":"硫精礦粉","ename":"Powdered pyrite concentrate;Pyrites concentrate,powder;Sulfur concentrate,powder","bm":"硫精礦"},
{"cpdh":"A02000101","page":"1","ca":"","mc":"磷礦石","ename":"Phosphorus ore","bm":""},
{"cpdh":"A02000111","page":"2","ca":"","mc":"磷礦砂","ename":"Phosphorite grit","bm":""},
{"cpdh":"A02000201","page":"2","ca":"","mc":"磷礦粉","ename":"Phosphate rock powder","bm":""},
{"cpdh":"A03000101","page":"2","ca":"12447-04-0","mc":"硼礦石","ename":"Ascharite;Boric ore","bm":"硼鎂礦"},
{"cpdh":"A04000101","page":"2","ca":"","mc":"鉀長石","ename":"Potash feldspar;Potassium feldspars","bm":""},
{"cpdh":"A04000201","page":"2","ca":"12003-63-3","mc":"長石粉","ename":"Feldspar powder","bm":""},
{"cpdh":"A04000301","page":"2","ca":"","mc":"光鹵石","ename":"Carnallite","bm":""}
]}
var vCpcj={"cjpage":[{"cjdh":"110634","cm":"拜耳光翌板材有限責任公司","pagen":"1701"},
{"cjdh":"110052","cm":"北京艾瑞斯水墨有限公司","pagen":"1701"},
{"cjdh":"110408","cm":"北京艾斯克醫藥技術開發有限公司","pagen":"1701"},
{"cjdh":"110010","cm":"北京愛德泰普膜制品廠","pagen":"1701"},
{"cjdh":"110165","cm":"北京安實創業科技發展有限公司","pagen":"1701"},
{"cjdh":"110134","cm":"北京安順達裝飾材料有限公司","pagen":"1701"},
{"cjdh":"110291","cm":"北京奧博星生物技術責任有限公司","pagen":"1701"},
{"cjdh":"110191","cm":"北京奧得賽化學有限公司","pagen":"1701"},
{"cjdh":"110454","cm":"北京奧克蘭防水工程有限責任公司","pagen":"1701"},
{"cjdh":"110520","cm":"北京奧森特種潤滑材料廠","pagen":"1701"}]}
奧森特
*/
function ObjSearch()
{
this.kw = '';
this.option = '';
this.displayId = '';
this.search = function()
{
// if(this.kw=='' || this.kw==null || this.kw.length<3)
// {
// alert('輸入長度不能為小於3!');
// return;
// }
// switch(this.option)
// {
// //企業
// case '0':
// this.searchCpcj();
// break;
// //產品
// case '1':
// this.searchCp();
// break;
// }
OutputHtml(this);
}
this.searchCpcj= function()
{
var jsonObj = new Array();
var kw = this.kw;
// $('#'+this.displayId).html('');
// var displayId = this.displayId;
$(vCpcj.cjpage).each(function(index,content){
// var cjdhy = content.cjdh;
// var cm = content.cm;
// var pagen = content.pagen;
if(content.cm.indexOf(kw)!=-1)
{
// $('#'+displayId).append("<a href='#'>"+cm+" | "+pagen+"</a><br/>");
jsonObj.push({"cm":content.cm,"pagen":content.pagen});
}
});
return jsonObj;
}
this.searchCp = function()
{
var jsonObj = new Array();
var kw = this.kw;
// $('#'+this.displayId).html('');
// var displayId = this.displayId;
$.each(vCp.cppage,function(index,content){
// var cpdh = content.cpdh;
// var page = content.page;
// var ca = content.ca;
// var mc = content.mc;
// var ename = content.ename;
// var bm = content.bm;
if(content.mc.indexOf(kw)!=-1)
{
// $('#'+displayId).append("<a href='#'>"+mc+" | "+page+"</a><br/>");
jsonObj.push({"mc":content.mc,"page":content.page});
}
});
return jsonObj;
}
}

<!--//
function __$$(id){return document.getElementById(id);} //定義獲取ID的方法
function GotoPage(num){ //跳轉頁
Page = num;
OutputHtml(os);
}
var PageSize = 20; //每頁個數
var Page = 1; //當前頁碼
function OutputHtml(){
//選擇的企業還是產品
var vobj = arguments[0];
var vtmp1=vobj.option;
var siteList = '';
switch(vtmp1)
{
//企業
case '0':
siteList = os.searchCpcj();
var obj = eval(siteList); //獲取JSON
//var sites = obj.cjpage;
var sites = obj;
break;
//產品
case '1':
siteList = os.searchCp();
var obj = eval(siteList); //獲取JSON
// var sites = obj.cppage;
var sites = obj;
break;
}
//獲取分頁總數
var Pages = Math.floor((sites.length - 1) / PageSize) + 1;

if(Page < 1)Page = 1; //如果當前頁碼小於1
if(Page > Pages)Page = Pages; //如果當前頁碼大於總數
var Temp = "";

var BeginNO = (Page - 1) * PageSize + 1; //開始編號
var EndNO = Page * PageSize; //結束編號
if(EndNO > sites.length) EndNO = sites.length;
if(EndNO == 0) BeginNO = 0;

if(!(Page <= Pages)) Page = Pages;
__$$("total").innerHTML = "總頁數:<strong class='f90'>" + sites.length + "</strong> 顯示:<strong class='f90'>" + BeginNO + "-" + EndNO + "</strong>";

//分頁
if(Page > 1 && Page !== 1){Temp ="<a href='javascript:void(0)' onclick='GotoPage(1)'><<第一頁</a> <a href='javascript:void(0)' onclick='GotoPage(" + (Page - 1) + ")'>上一頁</a> "}else{Temp = "<<第一頁 上一頁 "};

//完美的翻頁列表
var PageFrontSum = 3; //當頁前顯示個數
var PageBackSum = 3; //當頁後顯示個數

var PageFront = PageFrontSum - (Page - 1);
var PageBack = PageBackSum - (Pages - Page);
if(PageFront > 0 && PageBack < 0)PageBackSum += PageFront; //前少後多,前剩余空位給後
if(PageBack > 0 && PageFront < 0)PageFrontSum += PageBack; //後少前多,後剩余空位給前
var PageFrontBegin = Page - PageFrontSum;
if(PageFrontBegin < 1)PageFrontBegin = 1;
var PageFrontEnd = Page + PageBackSum;
if(PageFrontEnd > Pages)PageFrontEnd = Pages;

if(PageFrontBegin != 1) Temp += '<a href="javascript:void(0)" onclick="GotoPage(' + (Page - 10) + ')" title="前10頁">..</a>';
for(var i = PageFrontBegin;i < Page;i ++){
Temp += " <a href='javascript:void(0)' onclick='GotoPage(" + i + ")'>" + i + "</a>";
}
Temp += " <strong class='f90'>" + Page + "</strong>";
for(var i = Page + 1;i <= PageFrontEnd;i ++){
Temp += " <a href='javascript:void(0)' onclick='GotoPage(" + i + ")'>" + i + "</a>";
}
if(PageFrontEnd != Pages) Temp += " <a href='javascript:void(0)' onclick='GotoPage(" + (Page + 10) + ")' title='後10頁'>..</a>";

if(Page != Pages){Temp += " <a href='javascript:void(0)' onclick='GotoPage(" + (Page + 1) + ");'>下一頁</a> <a href='javascript:void(0)' onclick='GotoPage(" + Pages + ")'>最末頁>></a>"}else{Temp += " 下一頁 最末頁>>"}

__$$("pagelist").innerHTML = Temp;

//輸出數據

if(EndNO == 0){ //如果為空
__$$("pagelist").innerHTML='';
__$$("content").innerHTML = "<h1>沒有數據</h1>";
return;
}
var html = "";

for(var i = BeginNO - 1;i < EndNO;i ++){
switch(vtmp1)
{
//企業
case "0":
html += "<a href='#' rel='bookmark' title=" +sites[i].cm+ ">";
html += "<p class='url'><span>" +sites[i].cm+ " | "+sites[i].pagen +"</span></p></a>";
break;
//產品
case "1":
// if(sites[i].mc.indexOf(vobj.kw)==-1) continue;
html += "<a href='#' rel='bookmark' title=" +sites[i].mc+ ">";
html += "<p class='url'><span>" +sites[i].mc+ " | "+sites[i].page +"</span></p></a>";
break;
}
}
__$$("content").innerHTML = html;
clickShow(); //調用鼠標點擊事件

//鍵盤左右鍵翻頁
document.onkeydown=function(e){
var theEvent = window.event || e;
var code = theEvent.keyCode || theEvent.which;
if(code==37){
if(Page > 1 && Page !== 1){
GotoPage(Page - 1);

}
}
if(code==39){
if(Page != Pages){
GotoPage(Page + 1);
}
}
}

//鼠標滾輪翻頁
function handle(delta){
if (delta > 0){
if(Page > 1 && Page !== 1){
GotoPage(Page - 1);
}
}
else{
if(Page != Pages){
GotoPage(Page + 1);
}
}
}
function wheel(event){
var delta = 0;
if (!event) /* For IE. */
event = window.event;
if (event.wheelDelta) { /* IE或者Opera. */
delta = event.wheelDelta / 120;
/** 在Opera9中,事件處理不同於IE
*/
if (window.opera)
delta = -delta;
} else if (event.detail) { /** 兼容Mozilla. */
/** In Mozilla, sign of delta is different than in IE.
* Also, delta is multiple of 3.
*/
delta = -event.detail / 3;
}
/** 如果 增量不等於0則觸發
* 主要功能為測試滾輪向上滾或者是向下
*/
if (delta)
handle(delta);
}
/** 初始化 */
if (window.addEventListener)
/** Mozilla的基於DOM的滾輪事件 **/
window.addEventListener("DOMMouseScroll", wheel, false);
/** IE/Opera. */
window.onmousewheel = document.onmousewheel = wheel;


}
//獲取鏈接地址和網站名稱
function showLink(source){
var siteUrl = __$$("siteurl");
var siteName = __$$("sitename");
var description = __$$("description");

if(source.getAttribute("rel") == "bookmark"){
var url = source.getAttribute("href");
var title = source.getAttribute("title");
siteUrl.innerHTML = "<a href='" + url + "' target='_blank'>"+ url +"</a>";
siteName.innerHTML = title;
}

}
//鼠標點擊事件
function clickShow(){
var links = __$$("content").getElementsByTagName("a");
for(var i=0; i<links.length; i++){
var url = links[i].getAttribute("href");
var title = links[i].getAttribute("title");
links[i].onclick = function(){
// showLink(this);
return false;
}
}
}
//-->

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