javascript - 如何使用 javascript 在行尾添加连字符

标签 javascript jsp

这是我的编码。 使用css作为Hyphenate类的结果,结果显示在result1中。 然而,我想要的是结果显示在result2中。 另外,在 result1 中,当只有几个单词时,单词之间会出现很大的间隙,这真的很难看。 现在有人如何解决这个问题吗?

--编辑问题--

我在这里问的是:我有一个用于输入的文本区域和一个用于结果的 div 标签。 每当有一个单词到达右边界时,该单词就会换成新行,但是,目标就像结果 2 所示的那样。 希望现在任何人都能理解。谢谢。

Div 标签的宽度=170px

.hyphenate{ 
    text-wrap: suppress; 
    word-break: break-all; 
    word-wrap: break-word; 
    white-space: -moz-pre-wrap; 
    white-space: pre-wrap; 
    white-space: -o-pre-wrap; 
    white-space: pre-line; 
    width:170px; 
    text-align:justify; 
}

-

<script type="text/javascript">
    function copyIt() {
        var x = document.getElementById("textarea").value;
        document.getElementById("result1").innerHTML = x;
    }
</script>

-

<table border=2 width="160"> 
    <tr>
        <td colspan=2>
            <h1>Hyphenator Tester</h1> 
        </td>
    </tr> 
    <tr> 
        <td width="160" valign="top"> 
            <textarea id="textarea" rows=5 cols=20 onkeyup="copyIt()">
                This is the preview of something What
            </textarea> 
            <br><br>Result:<br>
            <div id="result1" class="hyphenate"> 
                This is the preview of something what I've wrote earlier
            </div>
            <br>Result wanted:<br>
            <div id="result2" class="hyphenate">
                This is the preview of some- thing what I've wrote earlier
            </div> 
        </td>
    </tr> 
</table>

最佳答案

尝试Hyphenator.js .

关于javascript - 如何使用 javascript 在行尾添加连字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1919676/

相关文章:

javascript - dojo 存储/内存查询 - 不等于

javascript - jQuery 在单击功能上添加和删除类问题

java - 无法调用自定义标签jsp

JSP 是ThreadSafe 默认的

javascript - 为什么 Javascript 字符串替换会颠倒从右到左语言的词序?

javascript - Vue.Js如何使用id获取prop属性的值

javascript - 为什么我无法调用 ngFor 内部的成员函数?

java - 使用 DAO/servlet 中的表单/参数过滤数据

java - 如何从项目结构中读取文件

java - 使用jsp和servlet删除表行