简体   繁体   English

将新样式应用于文本时,保留textarea的换行符

[英]Preserving line breaks from textarea when applying new styles to the text

I need to output the user-entered contents of a textarea to the page while keeping line breaks intact (which is working). 我需要将文本区域的用户输入内容输出到页面,同时保持换行符完好无损(正在运行)。 Then I need to apply a certain style to different words without losing the linebreaks (which is not working. 然后,我需要对不同的单词应用某种样式,而又不丢失换行符(这不起作用。

I have figured everything out except when I go to apply the style the line breaks are lost. 我已经弄清楚了一切,除了我要应用样式时,换行符丢失了。 Here is the Jsfiddle . 这是Jsfiddle

I am outputting what the user writes in the textarea to the screen with the following code: 我使用以下代码将用户在文本区域中写入的内容输出到屏幕:

$(window).load(function(){
$('.content:not(.focus)').keyup(function(){                 


var value = '<h3>' + $(this).val() + '</h3>';
var contentAttr = $(this).attr('name');

$('.'+contentAttr+'').html(value.replace(/\r?\n/g,'<br/>'));

})

});

Then when I user presses various buttons I am calling the following function. 然后,当用户按下各种按钮时,我将调用以下功能。 It is here that I somehow lose the line breaks: 我在这里以某种方式失去了换行符:

function myFunction(m1,m2,m3,m4,m5,m6,m7,m8,m9)
{
var words = $('h3').text().split(' ');
var numWords = words.length;
for (i=0; i<numWords; i++) {
if (i%m1 == 0 || i%m2 == 0 || i%m3 == 0 || i%m4 == 0 || i%m5 == 0 || i%m6 == 0 || i%m7 == 0 || i%m8 == 0 || i%m9 == 0) {
words[i] = '<span>' + words[i] + '</span>';

}
}


$('h3').html(words.join(' '));

The style I have is 我的风格是

h3 span {
font-weight: bold;
color: black;
background: black;
}


h3 span:hover {
color: white;
background: black;
} 

Also, please forgive me. 另外,请原谅我。 I am not a pro at javascript and have figured out how to do what I have done so far by researching stackoverflow and jsfiddle.net. 我不是javascript的专业人士,并且通过研究stackoverflow和jsfiddle.net找出了如何做到目前为止的工作。 I think the solution might not be far away but I cannot figure it out. 我认为解决方案可能并不遥远,但我无法弄清楚。 Thank you so much for helping out! 非常感谢您的帮助!

JSfiddle example JSfiddle示例

[UPDATED FIDDLE LINK and JS] Take a look at this [更新的FIDDLE LINK和JS]看看这个

I believe that the problem is that you are stripping out your 我认为问题在于您正在剥离自己的
tags when you are calling the .text() function. 调用.text()函数时的标记。

One way to solve for this would be to parse them in and out with a marker, like this: 解决此问题的一种方法是使用标记来解析它们,如下所示:

function myFunction(m1, m2, m3, m4, m5, m6, m7, m8, m9) {
    var $h3 = $('h3');
    var marker = ' ### ';
    var wordsHtml = $('h3').html().replace(/(\<br(\s)*(\/)*\>)/g, marker);
    var groups = wordsHtml.split(marker);
    var newHtml = '';
    for (var g = 0; g < groups.length; g++) {
        var words = groups[g].split(' ');
        for (var i = 0; i < words.length; i++) {
            if (i % m1 == 0 || i % m2 == 0 || i % m3 == 0 || i % m4 == 0 || i % m5 == 0 || i % m6 == 0 || i % m7 == 0 || i % m8 == 0 || i % m9 == 0) {
                newHtml += ' <span>' + words[i] + '</span>';

            }
            else {
                newHtml += words[i];
            }
        }
        // if not last group
        if (g != (groups.length - 1)) {
            //add a br tag
            newHtml += '<br />';
        }
    }
    $h3.html(newHtml);

};

Here is an update to YOUR fiddle: 这是您的小提琴的更新:

jsFiddle Update jsFiddle更新

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

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