2012-02-21 28 views
3

一個良好的開端可以通過類codemirror發現textareas嗎?

我這個問題「Codemirror - Use on multiple textareas?」幾年前問一個很好的答案。但是,它仍然需要一個ID作爲參數。 ID是唯一的。通過繼承,而不是ID

查找textarea的

有多個文字區域時,一些HTML和一些與在同一頁上的CSS,這將是很好添加一個類,而不是一個ID的。

<p>Some content</p> 

<textarea class="my_codemirror_html"> 
</textarea> 

<p>Some content</p> 

<textarea class="my_codemirror_html"> 
</textarea>' 

如果我可以使用jQuery它,它的罰款。無論如何,我在頁面上使用它。

更新2012-02-21 - 幾乎有

我發現這個職位上jsFiddle。唯一缺少的是我無法使它在textareas上工作。

+0

你可以這樣做。你的選擇是: textarea.my_codemirror_html { } – Undefined 2012-02-21 09:20:07

回答

6

我解決了它通過添加一個ID與jQuery到所有textareas。

jQuery(document).ready(function($) { 
      var code_type = ''; 
      $('.code-html').each(function(index) { 
       $(this).attr('id', 'code-' + index); 
       CodeMirror.fromTextArea(document.getElementById('code-' + index), { 
         mode: "text/html", 
         lineNumbers: true, 
         tabMode: "indent" 
        } 
       ); 

      }); 
     }); 
+0

工程很好,謝謝。 – Susan 2012-04-06 08:29:59

7

這將是與不太複雜一個簡單的解決方案。

$('.my_codemirror_html').each(function(index, elem){ 
     CodeMirror.fromTextArea(elem, {/*options*/}); 
}); 
+1

這應該是我認爲的答案,找不到比這更好的解決方案。它比標記的答案要複雜得多 – Crecket 2015-10-24 00:33:37

相關問題