2012-04-24 145 views
-1

我正在做一個平行段落突出顯示的並行文本。這是大約一半完成,在這裏:http://tinyurl.com/humecz2e自動編號增加編碼html?

問題是,它是非常非常繁瑣的手動編輯html,使每個段落兩側增加數字。例如:

<div class="indented numbered"> 
<label class="p2"> 
Those who have denied the reality of moral distinc&shy;tions, may be ranked among the disingenuous disputants; nor is it conceivable, that any human creature could ever seriously believe, that all characters and actions were alike entitled to the<span class="epagno"></span> affection and regard of everyone. The difference, which nature has placed between one man and another, is so wide, and this difference is still so much farther widened, by education, example, and habit, that, where the opposite extremes come at once under our apprehension, there is no scepticism so scrupulous, and scarce any assurance so determined, as absolutely to deny all distinction between them. Let a man&rsquo;s insensibility be ever so great, he must often be touched with the images of <strong>right</strong> and <strong>wrong</strong>; and let his prejudices be ever so obstinate, he must observe, that others are susceptible of like impressions. The only way, therefore, of converting an antagonist of this kind, is to leave him to himself. For, finding that nobody keeps up the controversy with him, it is probable he will, at last, of himself, from mere weariness, come over to the side of common sense and reason. 
</label> 
</div> 

<div class="indented numberedlower"><div class="sbnumbered"> 
<label class="p3"> 
There has been a controversy started of late, much better worth examination, concerning the general foundation of <strong>morals</strong>; whether they be derived from <strong>reason</strong>, or from <strong>sentiment</strong>; whether we attain the knowledge of them by a chain of argument and induction, or by an immediate feeling and finer internal sense; whether, like all sound judgment of truth and falsehood, they should be the same to every rational intelligent being; or whether, like the perception of beauty and deformity, they be founded entirely on the particular fabric and constitution of the human species. 
</label> 
</div></div> 

我想這樣做自動而不是手動,或者自動和手動一些組合。但我不知道該怎麼做。我有AutoHotKey,但我不確定它是一個理想的程序使用,或者甚至如何讓它增加數字。

任何幫助,將不勝感激。謝謝!

+0

您是否在尋找[CSS Counters](https:// deve loper.mozilla.org/en/CSS_Counters)? – user123444555621 2012-04-24 02:50:18

+0

也許可以解釋爲什麼你需要在類中增加數字,這就是你想要達到的目標,我們可以想辦法做到這一點,而不必增加類名稱。 – 2012-04-24 02:50:55

+0

@ Pumbaa80不,我不這麼認爲。數字需要寫入html本身。 – 97847658 2012-04-24 02:51:47

回答

1

你會想用javascript:

<script type="text/javascript"> 
    var elements = document.getElementsByTagName('label'); 
    for (var i in elements) { 
     var elem = elements[i]; 
     elem.className += " p"+i; 
    } 
</script> 

您可以考慮使用唯一的ID,而不是唯一的類名:

<script type="text/javascript"> 
    var elements = document.getElementsByTagName('label'); 
    for (var i in elements) { 
     var elem = elements[i]; 
     elem.id = "p"+i; 
    } 
</script> 

如果你只是希望以不同樣式的標籤,你可以嘗試使用nth-child CSS選擇器:

<style type="text/css"> 
    div label:nth-child(odd) { 
     background-color: red; 
    } 

    div label:nth-child(even) { 
     background-color: blue; 
    } 
</style> 
+0

我對JavaScript知之甚少,但我想知道它是否可以用來改變html本身,以及它是否符合兩種語言在其分段中並不總是一致的事實。謝謝! – 97847658 2012-04-24 03:10:11

+0

當頁面呈現時,JavaScript由瀏覽器評估。所以這些類將不會寫入您的.html文件,但是當頁面被加載時,瀏覽器將執行Javascript並動態地在指定的元素的類值中寫入。 – twaddington 2012-04-24 03:15:51