简体   繁体   English

Javascript插入奇怪的newLine字符

[英]Javascript inserting weird newLine character

I have an array named verses that contains strings created from an ajax response. 我有一个名为verses的数组,其中包含从ajax响应创建的字符串。 The array is created like this: 数组是这样创建的:

verses.push(splitStr[i].replace('\n',''));

Later I create some <span> elements and append the values from the array. 稍后,我创建一些<span>元素,并将值附加到数组中。 However the last element from array always has a strange behavior because it contains a hidden new line character in ASCII it has the code 10 (checked that in the console). 但是,数组中的最后一个元素总是有一个奇怪的行为,因为它包含一个ASCII隐藏的new line ,它的代码为10 (已在控制台中进行了检查)。 Now when I print in the console log the last element from the array a get this result: "string" . 现在,当我在控制台中打印日志时,数组中的最后一个元素将得到以下结果: "string" Then I create the span element that contains this string and later when I retrieve back the value from span I get: 然后,我创建包含此字符串的span元素,稍后当我从span取回值时,我得到:

"string
"

That's because when creating the span it always inserts this strange new line character. 那是因为在创建跨度时,它总是插入这个奇怪的new line So in the source code the span looks like: 因此,在源代码中,跨度如下所示:

<span class="answer" onclick="checkAnswer(this)">potopului
</span>

You can see that the closing tag is on the next line. 您可以看到结束标记在下一行。

In the console I used some break points and I got this value extracted from span: "string↵" 在控制台中,我使用了一些断点,并从span中提取了此值: "string↵"

Now, has anyone any idea why when creating this span javascript inserts a new line character and why does it happen only with the last element from array? 现在,有谁知道为什么在创建此span javascript时会插入new line以及为什么仅在数组中的最后一个元素上会发生这种情况?

The code used to create the span elements: 用于创建span元素的代码:

for (var i = 0; i < 3; i++) {
        if (i!=phtml) {
            tags.push("<span class='answer' onclick='checkAnswer(this)'>"+generateRandom(pos)+'</span>'); 
            //generate a  string from the array different from the one with position pos
        }else{
            tags.push("<span class='answer'  onclick='checkAnswer(this)'>"+verses[pos].replace('\n','')+'</span>');
        }
    }
    var output='';
    for (var i = 0; i < tags.length; i++) {
        output = output + tags[i];
    }
    document.getElementsByClassName('v-options')[0].innerHTML = output;

The checkAnswer() function: checkAnswer()函数:

function checkAnswer(e){
    var text = e.innerHTML;
    var input = document.getElementsByClassName('hidden-word')[pos];
    if (text == verses[pos]) {
        input.value = verses[pos];
        input.setAttribute('disabled','');
    }
    console.log(":"+text.substring(0, text.length-1) + ":"+verses[pos]+":");
}

Sometimes line breaks are printed in different styles. 有时换行符以不同的样式打印。 Commonly it is a combination of two special characters \\n and \\r so you just need to check all possible types of newline combinations or their lost broken parts. 通常,它是两个特殊字符\\n\\r的组合,因此您只需要检查所有可能的换行符组合类型或丢失的破损部分。 Try to change your RegExp to match them all : 尝试更改您的RegExp以使其全部匹配:

splitStr[i].replace(/\n\r|\n|\r/g, '')

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

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