gpt4 book ai didi

javascript - 无法让 Javascript 函数在演示中运行

转载 作者:行者123 更新时间:2023-11-28 11:11:05 25 4
gpt4 key购买 nike

function trim(str,options){
var string = str.replace(/^\s\s*/, ''),
ws = /\s/,
i = str.length,
j = -1;
if(options==="begin"){while(ws.test(string.charAt(++j)));return string.slice(j,i);}
if(options==="end"){while(ws.test(string.charAt(--i)));return string.slice(j+1,i+1);}
while(ws.test(string.charAt(--i)));while(ws.test(string.charAt(++j)));
return string.slice(j, i + 1);
}

此函数是 C# 中 Trim() 方法的实现。

我添加了用于删除开头和结尾处的空格的选项。问题是,我无法让它在演示中工作。

我所做的是:

var a = "           zareaerar  arzare        ";
var b = "aezze azeze a ";
var c = "azrazza rzrzrzrp"
var d = " aezzaeazeazeaz azez ";

document.write('<p style="backround:#ff0000">',trim(a,"begin"),'</p>','<br />');
document.write('<p style="backround:#ff0000">',trim(b,"begin"),'</p>','<br />');
document.write('<p style="backround:#ff0000">',trim(c,"begin"),'</p>','<br />');
document.write('<p style="backround:#ff0000">',trim(d,"begin"),'</p>','<br />');

首先,没有出现背景颜色,并且字符串似乎也失去了字符串中间的空格......

这仅适用于用户输入吗?现在字符串会自动 trim 吗?

最佳答案

1) 由于拼写错误,您没有看到背景:backround 而不是 background

2) 在 HTML 中,多个空格没有任何意义。如果要在段落中的单词之间插入不间断的空格,则需要使用  

Reference (W3 spec) :

Note that a sequence of white spaces between words in the source document may result in an entirely different rendered inter-word spacing (except in the case of the PRE element). In particular, user agents should collapse input white space sequences when producing output inter-word space. This can and should be done even in the absence of language information (from the lang attribute, the HTTP "Content-Language" header field (see [RFC2616], section 14.12), user agent settings, etc.).

The PRE element is used for preformatted text, where white space is significant.

关于javascript - 无法让 Javascript 函数在演示中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1293064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com