【问题标题】:Javascript / Forcing a line break after the point if I have one wordJavascript / 如果我有一个单词,则在该点之后强制换行
【发布时间】:2015-09-05 17:12:07
【问题描述】:

如图所示,当我在 dot 后面有一个单词时,我会强制换行,这是出于审美问题,但尤其是出于良好阅读的考虑。

有可能吗?

http://i.stack.imgur.com/EkcuR.jpg

【问题讨论】:

标签: javascript jquery css line-breaks


【解决方案1】:

这个怎么样:

$('p').html($('p').text().replace(/\.\s/g, '.<br>'));

这里是它的 JSBin 演示: http://jsbin.com/sapate

【讨论】:

  • 不完全是。因此,只要有一个点,它就会添加一个新的断线。
  • @david rosa,这样,只要有一个点,你就添加一个新行。但我要求是每增加一个新行(点+“靠近右边的没有单词的单字”)这个概念类似于排版中的寡妇和孤儿
【解决方案2】:

在 css 中,我们目前没有任何基于单词的伪元素。最好有类似的东西:

section div::first-word {
  page-break-after: always;
}

我认为一个解决方案是用 jquery 来做到这一点:

<script>
$(document).ready(function($) {
    $('span').each(function() {
        var word = $(this).html();
        var index = word.indexOf(' ');
        if(index == -1) {
            index = word.length;
        }
        $(this).html('<span class="first-word">' + word.substring(0, index) + '</span><br/>' + word.substring(index, word.length));
    });
});
</script>
<style>
    section{
        width:500px;
        float:left;
        font:14px;
    }
    .first-word { 
        font-style:italic;
    }
</style>

<section>
<span>
Lorem ipsum dolor sit amet, in luctus, at eros nec turpis, malesuada massa vel purus nonummy, lorem quis neque.
</span>
<span>
Neque ac, ut nunc dui mattis sollicitudin arcu, sed sollicitudin scelerisque enim a. 
</span>
<span>
Praesent sit urna ipsum, tortor integer elit in convallis pulvinar mauris.  
</span>
</section>

http://jsfiddle.net/MadalinaTn/n7zpuLbo/5/

【讨论】:

【解决方案3】:

这也有效(*同样的情况 - 你不想在一个单词之后出现文本中断。我的解决方案是选择前两个单词,用一个类将它们包装起来,不要让那个文本中断):

<style>
    section{
        width:500px;
        float:left;
        font:14px;
    }
    .first-two-words2 { 
        font-style:italic;
        white-space: nowrap;
    }
</style>
<script>
$(document).ready(function($) {
$('span').html(function (i, html) {
    return html.replace(/(\w+\s\w+)/, '<span class="first-two-words2">$1</span>')
});
});
</script>
<section>
<span>
Lorem ipsum dolor sit amet, in luctus, at eros nec turpis, malesuada massa vel purus nonummy, lorem quis neque,lorem quis neque, lorem quis zz.
</span>
<span>
Neque ac, ut nunc dui mattis sollicitudin arcu, sed sollicitudin scelerisque enim a. 
</span>
<span>
Praesent sit urna ipsum, tortor integer elit in convallis pulvinar mauris.  
</span>
</section>

http://jsfiddle.net/MadalinaTn/me76t9vv/1/

【讨论】:

  • 工作,类似:$('p').each(function(){ var string = $(this).html(); string = string.replace(/ ([^ ]*)$/,'&amp;nbsp;$1'); $(this).html(string); }); 但仅适用于最后一行
【解决方案4】:

在您编辑问题后,我了解到您实际上不希望在一个单词后出现文本中断。我的解决方案是选择前两个单词,用一个带有类的 span 将它们包装起来,不要让那个文本中断:

<style>
    section{
        width:500px;
        float:left;
        font:14px;
    }
    .first-two-words { 
        font-style:italic;
        white-space: nowrap;
    }
</style>
<script>
$(document).ready(function($) {
    $('span').each(function() {
        var words = $(this).html();
        var n = words.split(" ");   
        var i = n[0]+n[1];
        index = i.length + 1;
        $(this).html('<span class="first-two-words">' + words.substring(0, index) + '</span>' + words.substring(index, words.length));
    });
});
</script>
<section>
<span>
Lorem ipsum dolor sit amet, in luctus, at eros nec turpis, malesuada massa vel purus nonummy, lorem quis neque,lorem quis neque, lorem quis zz.
</span>
<span>
Neque ac, ut nunc dui mattis sollicitudin arcu, sed sollicitudin scelerisque enim a. 
</span>
<span>
Praesent sit urna ipsum, tortor integer elit in convallis pulvinar mauris.  
</span>
</section>

http://jsfiddle.net/MadalinaTn/btngqcpq/1/

【讨论】:

  • 它工作得很好,我想我不能用一个段落“p”而不是“span”来使用相同的代码?非常感谢
  • 欢迎您!我相信你可以更新代码。这只是一个解决方案。
猜你喜欢
  • 2014-10-27
  • 2017-11-14
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
  • 2014-08-25
  • 1970-01-01
相关资源
最近更新 更多