用于多个ID的简单MDE编辑器

时间:2017-09-20 17:52:42

标签: javascript jquery

我正在努力让这个插件适用于单个表单上的多个ID。

插件...... Simple MDE Editor

代码如下......

<!-- Include the required files -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>

<!-- Start simple MDE -->
<script>
    var simplemde = new SimpleMDE({ 
        element: document.getElementById("MyID") 
    });
</script>

到目前为止,我尝试过: 1 - getElementsByClassName ...没有运气 2 - querySelectorAll ...没有运气 3 - 逐个列出元素......

document.getElementById("id1") , 
	document.getElementById("id2") ,
	document.getElementById("id3") ,
	document.getElementById("id4")

也没有运气。如何为同一形式的多个元素执行此操作?

2 个答案:

答案 0 :(得分:1)

  

element:要使用的textarea的DOM元素。默认为页面上的第一个textarea

这意味着您需要为每个元素实例化多个 SimpleMDE

var simplemde = {};

$('#id1, #id2, id3').each(function(idx, ele) {
    simplemde[ele.id] = new SimpleMDE({
        element: ele
    });
})

&#13;
&#13;
var simplemde = {};

$('[id^="MyID"]').each(function(idx, ele) {
    simplemde[ele.id] = new SimpleMDE({
        element: ele
    });
})
&#13;
.CodeMirror {
    height: 50px !important;
    min-height: 50px !important;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>


<textarea id="MyID1">MyID1</textarea>
<textarea id="MyID2">MyID2</textarea>
<textarea id="MyID3">MyID2</textarea>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果您要将SimpleMDE应用于所有 TextArea,请尝试以下操作:

$('textarea').each(function() {
      var simplemde = new SimpleMDE({
         element: this,
      });
   });

致谢!