防止HTML删除连续空格 [英] Prevent HTML Removing Successive spaces

查看:56
本文介绍了防止HTML删除连续空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSP页面中遇到问题,我必须在屏幕上基于字符串显示生成的消息.一切正常,直到其中一个帐号包含两个空格为止.

I have an issue in a JSP page where I have to display a generated message onscreen based on a string. It all works fine until one of the account numbers contains two spaces.

所以,我有这个HTML:

So, I have this HTML:

    <logic:notEqual name="migrationsMessage" value="">
        <div style="color:Red;font-weight:bold">
            <bean:write name="solasDetailsForm" property="migrationsMessage"/>
        </div>
    </logic:notEqual>

当字段migrationsMessage包含以下内容时:

When the field migrationsMessage contains this:

<input type="hidden" name="migrationsMessage" value="A  123456W has migrated to A 123456.">

屏幕上的输出是这样:

"A 123456W has migrated to A 123456."

第一个A之后的第二个空格被删除.我试图将样式更改为这种样式,但没有帮助:

The second space after the first A is removed. I tried to alter the style to be this but it didn't help:

    <logic:notEqual name="migrationsMessage" value="">
        <div style="color:Red;font-weight:bold;white-space:pre">
            <bean:write name="solasDetailsForm" property="migrationsMessage"/>
        </div>
    </logic:notEqual>

任何想法出了什么问题吗?

Any ideas what is going wrong?

推荐答案

function encodeWhiteSpaces(str) {
   return str.split('').map(function(c) { return c === ' ' ? '&nbsp;' : c }).join('');
}

将字符串转换为数组( split ),然后创建一个新的arrray( map ),并将所有空格都转换为& nbsp; ,最后将数组连接回字符串( join ).

The string is converted to an array(split), then created a new arrray (map) with all white spaces converted to &nbsp;, finally join the array back to a string (join).

这篇关于防止HTML删除连续空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆