저는 프로그래머가 아니지만 바로 주변을 털어 내고 배우기를 좋아합니다. 다음과 같은 형식의정규 표현식으로 코드를 우아하게 만드는 데 도움이 필요합니다.
1) URL : 사용 가능한 페이지의 #를 출력 http://site.com/#!/show/me/stuff/1-12/
2)의 jQuery 매김 스크립트를 여기에 내가 가진거야. 각 번호는 href="#pageNumber"
의 링크입니다. 3) 모든 페이지 매김 링크에 바인딩되는 jQuery 스크립트. 기능 코드는 다음과 같습니다.
기본적으로 내 목표는 페이지 2를 선택하는 경우 내 URL의 1-12를 적절한 값으로 바꾸는 것입니다.
var pageId = this.href.split("#")[1];
// this gets the number from the pagination link (i.e.,
// http://site.com/#2 becomes 2)
var url = location.hash.split("/");
var url = url[url.length-2];
// this is my way of extracting "1-12" from the URL :)
var showFrom = parseFloat(url.split("-")[0]); // 1
var showTo = parseFloat(url.split("-")[1]); // 12
var itemsPerPage= (showTo-showFrom)+1; // 12
var newShowTo = (itemsPerPage*pageId); // 24
var newShowFrom = (itemsPerPage*pageId)-itemsPerPage+1; // 13
var newUrl = newShowFrom+"-"+newShowTo; // 13-24
location.hash = location.hash.replace(url, newUrl);
// http://site.com/#!/show/me/stuff/1-12/ now becomes
// http://site.com/#!/show/me/stuff/13-24/
가 어떻게이 더 우아하고 더 적은 단계로 할 수 있습니다
는 여기가 작동하지만 매우 추한입니다 요리 무엇인가?
- 정수를 사용하고 있으므로 parseFloat()가 아닌 parseInt()를 사용하십시오. –