Kaibo's first article: record a frontend's "change one" Function

Source: Internet
Author: User

Simple requirements: implement the "change one" button function, click it, and change to another batch of corresponding brands

 

At the beginning, we considered Using ajax to request data, and then filling in the specified location on the page through Js. However, I am too lazy to configure a request ing for this Ajax request separately, simply splice all the required content into a JSON string in the background and parse it directly at the front-end for paging.

The page is relatively simple, so the HTML code will not be available. The general idea is: the two "change one" Buttons correspond to two sets of data respectively, share one paging method, and pass in their respective pages, displays the content of the corresponding page number. Of course, there are a variety of ways to achieve "change one". Here I just want to record my own ideas, and I welcome criticism and suggestions, Pat bricks, and take good care of new people. 3q.

Save your saliva and go directly to the Code ~

1 $ (function () {2 3 // "change one" function home, Groceries click Counter 4 var jiajubrandcount = 0; 5 var zahuobrandcount = 0; 6 7 // initialize the home brand content 8 jiajubrandcount = clicktochangebrand ("jiajubrand", "jiajubrandval", jiajubrandcount); 9 // initialize the grocery brand content 10 zahuobrandcount = clicktochangebrand ("zahuobrand ", "zahuobrandval", zahuobrandcount); 11 12 // for a home change click 13 $ ("# jiajubrandchange "). click (function () {14 jiajubrandcount = clicktochangebrand ("jiajubr And "," jiajubrandval ", jiajubrandcount); 15}); 16 17 // for groceries, click 18 $ (" # zahuobrandchange "). click (function () {19 zahuobrandcount = clicktochangebrand ("zahuobrand", "zahuobrandval", zahuobrandcount); 20 }); 21 22 // brand "change one" function 23 // container: target container 24 // valsrc: container where values are stored 25 // brandcount: Count 26 function clicktochangebrand (container, valsrc, brandcount) {27 28 // $ ("#" + container ). empty (); // clear the existing content 29 var S = $ ("#" + valsrc ). val (); // Obtain all brand data 30 var OBJ = $. parsejson (S. tostring (); // convert to json31 var totalcount = obj. length; // The total number of records 32 var perpagecount = 20; // The number of pre-viewed records per page 33 var pagecount = 0; // the total number of pages 34 var actualperpagecount = 0; // The actual number of displays per page 35 var strtotal = ""; // buffer variable 36 37 var temprs = totalcount % perpagecount; // calculate the total number of pages 38 If (temprs! = 0) {39 pagecount = parseint (totalcount/perpagecount) + 1; 40} 41 else {42 pagecount = parseint (totalcount/perpagecount ); 43} 44 45 // obtain the actual number of records to be displayed each time. If the last page contains less than 46 If (brandcount * perpagecount + perpagecount> totalcount) {47 actualperpagecount = totalcount-brandcount * perpagecount; 48} 49 else {50 actualperpagecount = perpagecount; 51} 52 53/* alert ("counter:" + brandcount ); 54 alert ("actual number of pages:" + actualperpagecount); 55 alert ("predetermined number of pages:" + perpagecount); 56 alert ("Total number of pages:" + pagecount ); */57 58 // get the content of the current page 59 for (I = brandcount * perpagecount; I <brandcount * perpagecount + actualperpagecount; I ++) {60 // OBJ [I]. title is the title, OBJ [I]. name is the category id61 strtotal + = "<a href = ''target = '_ blank'>" + OBJ [I]. title + "</a>"; 62} 63 64 // when the counter is greater than or equal to the number of pages, the system returns to 65 brandcount ++; 66 If (brandcount> = pagecount) {67 brandcount = 0; 68} 69 70 // fade out 71 $ ("#" + container ). empty (). append (strtotal ). hide (). stop (True, true ). fadein ("slow"); 72 73 return brandcount; 74} 75 });

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.