简体   繁体   English

TinyMCE如何动态获取文本区域中的换行符

[英]TinyMCE how to dynamically get line breaks in a textarea

I need advice on how I can create dynamic line breaks ideally with an <li> inside TINY. 我需要有关如何在TINY内使用<li>理想地创建动态换行符的建议。 I have tried using an each loop and also a for loop, and the values just do not appear in the text area. 我已经尝试过使用each循环和for循环,并且这些值只是未出现在文本区域中。 BUT, if I just add them to the text area with a val() they go in fine, BUT as just one long string. 但是,如果我只是使用val()将它们添加到文本区域,它们会很好,但只是一个长字符串。

The text area has an id of wo_materials. 文本区域的ID为wo_materials。 I'm successfully getting my text into Tiny like this: 我成功地将文本输入Tiny,如下所示:

        $('#wo_materials').val(materials);

        tinymce.init({
            selector:'textarea'
        });

And I get a nice row of text values: 而且我得到了一行漂亮的文本值:

TinyMCE中的值

The materials value is an array. 材料值是一个数组。 If I look at it in the console it looks like this: 如果我在控制台中查看它,则如下所示:

0: BP #15 Plain Felt 36" 
1:  Duraflo Weatherpro 50 
2:  1 1/4 Coil Nails - box

Thanks ! 谢谢 !

如果您只在初始化tinymce之前修改文本区域的值,那么这可能对您有用:

$('#wo_materials).val(materials.join('<br/>'));

This works. 这可行。 The secret is adding the incrementing var "text" to the for loop and then wrapping the object / array in an <li> 秘诀是将递增的var“ text”添加到for循环中,然后将对象/数组包装在<li>

var materials= JSON.parse(localStorage.getItem('materials'));
var text=" "; 
materials.length;
function workorders(){
            for (i = 0; i < materials.length; i++) {
               text += "<li>"+materials[i]+"</li>";
            }
            $('#wo_materials').val("<li>"+text+"</li>");
        }

        workorders();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM