2012-10-15 72 views
1

跳轉到頁面菜單我有一個包含每個部分的章節標題和頁面數量的字符串如下:創建一個部分索引,並從字符串

string = "section1;section2;section3;section4|2 3 1 4" 

我想能夠創建一個部分索引和使用標題作爲整個文檔中適當頁面的鏈接。

有什麼建議嗎?我最初的想法是將字符串拆分爲節標題和頁數,然後拆分這些並將標題追加到無序列表中?這將照顧部分標題,但頁面的數量呢?

var options = string.split('|'); 

    var sections = options[0]; 

    var section_titles = sections.split(';'); 

    for (a=0;a<section_titles.length;a++){ 
    $('ul').append('<li><div class="title">'+sections[a]+'</div></li>'); 
    }; 

    var counts = options[1]; 

    var section_counts = counts.split(' '); 

    ??????? 

請發送您的建議。

+0

我重讀了幾遍似乎無法理解你下一步想做什麼的問題。另外,我認爲'a elclanrs

+0

對不起,我基本上有一個應用程序,它將pdf文檔合併並柵格化爲一個文件。但是我想使用應用程序用來創建合併單個文件的目錄的文件,它使用這些文件來創建。我知道文檔標題和每個頁面的計數,但無法找到將它們列爲內容列表的方式,因此用戶可以單擊標題並轉到文檔中的該頁面。這是否更有意義? – jlukin

回答

0

這裏是結束了工作對我來說...

  • 我改變了字符串包括「稱號;計數」由分隔|
  • 我outputing這是一個無序列表
var str = "section1;2|section2;3|section3;1|section4:4" 
var d = 0; 
var ul = $('<ul>'); 
var sec = toc.split('|'); 
var a = 0; 
    for (a = 0; a < sec.length; a++) { 
var secArr = sec[a].split(';'); 
    var sectiontitle = secArr[0]; 
    var sectioncount = secArr[1] * 1; 
var jump = d; 
d = d + sectioncount; 
    if (sectiontitle == ''){ 
    } else { 
    $('#sections').find('ul').append('<li><div class="toc"><div  class="title">'+sectiontitle+'</div></div><span class="jumper" 
style="display:none;">'+jump+'</span></li>'); 
    } 
    } 
0

如果您的應用程序是一個Web應用程序,並且您不想在pdf文檔中創建索引,這將有所幫助。

最簡單的方法是利用錨點和鏈接。

閱讀這些請: link anchor

然後看到完整的代碼示例波紋管。您可以將其全部複製到test.html文件中,然後在瀏覽器中打開它以查看它的行爲。

<body> 

<script type="text/javascript"> 
document.write("\<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'>\<\/script>"); 
</script> 
<style> 
section{font-size:100px;} 
</style> 


First things first, your data setup seems odd. If you can change that string's setup, try going the JSON way. For example: 
<script> 
var jsonString = '{ "section1":2, "section2":3, "section3":1, "section4":4 }'; 

var objectFromJson = jQuery.parseJSON(jsonString); 
var ul = $('<ul>'); 
for (var prop in objectFromJson) 
{ 
    var li = $("<li>"); 
    var divTitle = $("<div>"); 
    divTitle.addClass("title"); 
    var titleLink = $("<a>"); 
    titleLink.text(prop); 
    titleLink.attr("href","#" + prop + "/" + objectFromJson[prop]) 
    divTitle.append(titleLink) 
    li.append(divTitle); 
    ul.append(li); 
    } 
$("body").append(ul); 
</script> 

If that is not possible, let's continue on what you have 
<script> 
var str = "section1;section2;section3;section4|2 3 1 4" 
var options = str.split('|'); 

var sections = options[0]; 
var numbers = options[1]; 

var section_titles = sections.split(';'); 
var numbersArr = numbers.split(' '); 

var ul = $('<ul>'); 
for (var a=0; a < section_titles.length; a++) 
{ 
    if(numbersArr.length > a) 
    { 
     var li = $("<li>"); 
     var divTitle = $("<div>"); 
     var titleLink = $("<a>"); 
     titleLink.text(section_titles[a]); 
     titleLink.attr("href","#" + section_titles[a] + "/" + numbersArr[a]) 

     divTitle.addClass("title"); 
     divTitle.append(titleLink) 
     li.append(divTitle); 
     ul.append(li); 
    } 
    } 
$("body").append(ul); 
</script> 


<section> 
<a name="section1/2">section1</a><br/><br/> 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at tristique est. Curabitur rhoncus consequat sagittis. Proin pharetra elit nec turpis rutrum a lobortis nisi ornare. Nulla nec nisl diam, vel semper orci. Phasellus iaculis nibh a velit pulvinar sagittis eu ut massa. Cras vestibulum, urna sed ultrices consequat, purus leo venenatis lorem, a bibendum nibh eros ut leo. Curabitur viverra urna id ante mattis ultrices. Nulla et velit enim, vel condimentum magna. Vestibulum ullamcorper consectetur porta. Maecenas eget dui nec turpis condimentum aliquet in in risus. 
</section> 

<section> 
<a name="section2/3">section2</a><br/><br/> 
Ut ac blandit nisi. Phasellus fringilla, est vel hendrerit mollis, dui sem sagittis justo, eget fermentum odio ipsum at sem. Sed tempor, nibh eu vulputate vehicula, lectus neque eleifend erat, at tincidunt orci dui a tellus. Vestibulum viverra urna in tellus vestibulum semper. Etiam volutpat sodales rutrum. Morbi elementum, lacus ut fermentum malesuada, ipsum sem viverra arcu, ut consequat libero dui et arcu. Cras ut libero nulla, nec rhoncus erat. Integer id enim id nulla sagittis congue. Integer mollis tempor viverra. Ut non nisi sem, et semper lacus. Vivamus turpis mi, hendrerit non pellentesque a, iaculis id nibh. Pellentesque nec arcu magna, at posuere eros. Duis eu quam at nulla porttitor aliquet. Vestibulum cursus mattis purus, a sodales mi rhoncus vitae. Phasellus quis urna nec orci rhoncus consequat. 
</section> 

<section> 
<a name="section3/1">section3</a><br/><br/> 
Donec pellentesque congue sollicitudin. Fusce aliquam, velit et cursus iaculis, nisl massa vulputate magna, vitae imperdiet felis tortor sit amet purus. Vivamus aliquet laoreet ligula quis aliquet. Vivamus fringilla purus ut lorem faucibus viverra. Sed accumsan tincidunt tempus. Donec id velit in dolor luctus gravida. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aenean sit amet nulla augue. Proin vel tincidunt est. Curabitur imperdiet elementum suscipit. Praesent dignissim magna eget sapien pretium vestibulum id at arcu. Integer egestas purus non. 
</section> 

<section> 
<a name="section4/4">section4</a><br/><br/> 
Donec pellentesque congue sollicitudin. Fusce aliquam, velit et cursus iaculis, nisl massa vulputate magna, vitae imperdiet felis tortor sit amet purus. Vivamus aliquet laoreet ligula quis aliquet. Vivamus fringilla purus ut lorem faucibus viverra. Sed accumsan tincidunt tempus. Donec id velit in dolor luctus gravida. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aenean sit amet nulla augue. Proin vel tincidunt est. Curabitur imperdiet elementum suscipit. Praesent dignissim magna eget sapien pretium vestibulum id at arcu. Integer egestas purus non. 
</section> 


</body> 
+0

很酷,感謝您的快速回復。我使用jquery循環滑塊將pdf格式化爲jpg圖像以供查看。我需要能夠轉到循環滑塊中的部分的頁碼,所以我不認爲錨會起作用。我想我需要以某種方式獲取該部分開始的頁碼數量,使其不超過循環滑塊的總頁數。 – jlukin

相關問題