通过< p>禁用自动修剪文字标签 [英] Disable auto-trimming of text by <p> tag
问题描述
我在var中有一些文本。 var text =地狱O
。文本的每个字符都包含在span标记中,给定一个id,然后附加到p标记的innerhtml中。然后使用函数对每个跨度进行样式设置(在事件上)。
问题是p标记开头或结尾的空格没有被突出显示。我不确定p标签是否忽略了这些空格,并且没有将它们包含在最后的段落中。但似乎第一个/最后一个空格没有被包含在内。
这是我的造型函数btw。
函数highlightLetter(typedletter ){
var targetId =h+(typedletter);
makeGreen = document.getElementById(targetId);
if(makeGreen){
console.log(makeGreen.innerText.charCodeAt(0));
if(makeGreen.innerText.indexOf(String.fromCharCode(10))== -1){
makeGreen.style.background =lightgreen;
}
}
}
?
谢谢 段落不呈现。此外,段落内部的空白序列也会被折叠。
如果你想让所有的空格都被渲染,使用 non-breaking space entity & nbsp;
。请注意,这将影响换行,并且可能会导致水平滚动,否则行将被换行。
您可以用所有普通空格字符替换非空格字符:
var text =Hell O;
text.replace(/ / g,& nbsp;);
或者,您也可以使用< pre> ...< / pre>
标签。
I have some text in a var. var text = " Hell O "
. each character of the text is wrapped in span tag , given an id and then appended to innerhtml of a p tag. Then with a function each span is styled (on an event).
Problem is that any spaces at the beginning or end of the p tag are not getting highlighted. I am not sure whether the p tag is ignoring the spaces and not including them in the final para . But it does seem that the first/last spaces are not being included.
This is my styling function btw.
function highlightLetter(typedletter) {
var targetId = "h"+( typedletter );
makeGreen = document.getElementById(targetId) ;
if (makeGreen) {
console.log(makeGreen.innerText.charCodeAt(0)) ;
if (makeGreen.innerText.indexOf(String.fromCharCode(10)) == -1 ) {
makeGreen.style.background = "lightgreen" ;
}
}
}
Any ideas ?
Thanks
Whitespace at the beginning and end of a paragraph isn't rendered. Also, sequences of whitespace within the paragraph are collapsed.
If you want all your whitespace to be rendered use non-breaking space entity
. Note that this will affect line breaking and may induce horizontal scroll where lines would otherwise be wrapped.
You can replace all ordinary whitespace characters with non-breaking space like this:
var text = " Hell O ";
text.replace(/ /g, " ");
Alternatively, you can also use <pre>...</pre>
tags.
这篇关于通过< p>禁用自动修剪文字标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!