最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable auto-trimming of text by <p> tag - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Jan 28, 2012 at 14:48 gyaani_guygyaani_guy 3,2099 gold badges44 silver badges53 bronze badges 1
  • Please post all the relevant code, minimally either with the code that adds tags to the data or the resulting HTML code. – Jukka K. Korpela Commented Jan 28, 2012 at 14:52
Add a ment  | 

1 Answer 1

Reset to default 7

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.

发布评论

评论列表(0)

  1. 暂无评论