简体   繁体   English

JS 问题,试图在单词之间添加空格

[英]JS issues, trying to add a space between words

I am having a little issue with my JS code.我的 JS 代码有点问题。

I have written the below but i cant seem to add spaces between the words?我已经写了以下内容,但我似乎无法在单词之间添加空格?

var textareas = document.getElementsByTagName("textarea");
var values = textareas [0].value ;
var vals = values.s
var result =  ""

for (i = 0 ; i < vals. length ; i ++ ) {
   if (vals [i] == '') {

  }
}

textareas[1].value = result;

as you can see that there are spaces between the numbers which would need to match the word spacings?如您所见,数字之间有空格需要匹配单词间距?

the text area is a HTML element, and i want to add spaces between the words so it pastes文本区域是 HTML 元素,我想在单词之间添加空格以便粘贴

this them their这是他们的

instead of代替

thisthemtheir in the text area这他们他们在文本区

Thanks谢谢

One thing is that if you have spaces in the source text (ie between the numbers), you need a space in the comparison.一件事是,如果源文本中有空格(即在数字之间),则在比较中需要一个空格。 So, it should be:所以,它应该是:

if (vals [i] == ' ') if (vals [i] == ' ')

and not并不是

if (vals [i] == '') if (vals [i] == '')

Also, change the line:另外,更改行:

result += [ ]结果 += [ ]

to read读书

result += ' '结果+=''

Here it is working with spaces:在这里它使用空格:

 var values = "42,54,53,43, ,42,54,57,49"; var vals = values.split(","); var result = "" for (i = 0; i < vals. length; i ++ ) { if (vals [i] == ' ') { result += ' ' } else { result += String.fromCharCode (31 + 127 - parseInt (vals [i])); } } console.log(result);

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

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