2014-09-19 173 views
0

我想讓NicEdi t僅適用於ID爲'r_desc'的所有textareas。目前,這僅發生在while循環中生成的第一個窗體實例,而不發生在其他窗體中。我究竟做錯了什麼?使NicEdit出現在給定相同ID的所有實例中

的JavaScript

<script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
    bkLib.onDomLoaded(function() { 
     nicEditors.editors.push(
      new nicEditor().panelInstance(
       document.getElementById('r_desc') 
      ) 
     ); 
    }); 
    </script> 

PHP/HTML

$i=0; 
while($i<5){ ?> 
<form id="content" name="content" method="POST" action="index.php"> 
    <textarea name="r_type" id="textarea"></textarea> 
    <textarea name="r_desc" id="r_desc"></textarea> 
    <textarea name="r_rate" id="textarea"></textarea> 
    <input type="submit" id="button" value="Update" /> 
</form> 
<? $i++; 
} 

我也曾嘗試:

bkLib.onDomLoaded(function() { 
    var textareas = document.getElementsByID("r_desc"); 
    for(var i=0;i<textareas.length;i++) 
    { 
     var myNicEditor = new nicEditor(); 
     myNicEditor.panelInstance(textareas[i]); 

    } 
}); 

回答

1

元素應該具有唯一的ID。 getElementById的實現知道這一點,只會返回與給定ID匹配的第一個元素。

您需要使用識別要轉換爲Nicedit

如果你使用類似getElementsByClassNamegetElementsByTagName,而不是元素的不同的方法(請注意,這些返回的元素不只是一個元素)

PHP :

$i=0; 
while($i<5){ ?> 
<form id="content" name="content" method="POST" action="index.php"> 
    <textarea name="r_type" class="to-nice"></textarea> 
    <textarea name="r_desc" ></textarea> 
    <textarea name="r_rate" class="to-nice"></textarea> 
    <input type="submit" id="button" value="Update" /> 
</form> 
<? $i++; 
} 

的JavaScript:

var els = document.getElementsByClassName("to-nice"); 
相關問題