【问题标题】:Limit the number of words in a div限制 div 中的单词数
【发布时间】:2014-06-25 11:09:46
【问题描述】:

有没有css属性可以限制一个div中显示的字符数。例如

<div class ="main-text">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>

但我只需要显示

<div class ="main-text">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
</div>

我正在使用带有推荐功能的 wordpress 网站。因此,如果任何客户添加了推荐,我只需要显示带有链接的前几个单词。请帮忙!!谢谢!!

【问题讨论】:

    标签: html css wordpress


    【解决方案1】:

    您可以使用 css 省略号。只需在文本溢出上使用它。它将截断以适应您框的宽度。

    http://davidwalsh.name/css-ellipsis

    http://css-tricks.com/snippets/css/truncate-string-with-ellipsis/

    .main-text {
      width: 250px;
    
    }
    
    .main-text p {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis; 
    }
    

    一个限制是您只能使用一行。

    【讨论】:

    • 一个建议,而不是在此处发布链接粘贴相关代码。
    • 这一行似乎太接近了。有什么建议让它超过一排吗?
    • @user3488898 很多人使用这个 jQuery 插件 dotdotdot.frebsite.nl (dotdotdot)。在纯 CSS 中是不可能的。
    【解决方案2】:

    您可以使用overflow 属性(实际上有几个-overflow-x, overflow-y 和overflow),您还必须以绝对单位设置width(读取像素或em,但是div 的强>不是百分比)。

    例如(CSS):

    div.main-text {overflow-x: hidden;width: 200px;} 
    /*This will hide everything, which goes outside of the 200px div*/
    

    您还可以使用简单的 PHP 代码来限制您的文本:

    if (!function_exists('chop_string')) {
    function chop_string($str, $len) {
        if (strlen($str) < $len)
            return $str;
    
        $str = substr($str,0,$len);
        if ($spc_pos = strrpos($str," "))
                $str = substr($str,0,$spc_pos);
    
        return $str . "Read more...";
    }   
    }
    

    将上述函数放在你的 Wordpress 主题的 functions.php 文件中并像这样使用它:

    &lt;?php echo chop_string('Bla bla bla', 5); ?&gt;

    上面的代码将只显示字符串中的 5 个字符和 Read more...。

    编辑:

    如果您想剪切来自 Wordpress 的标题或生成的内容,那么您必须编辑您的 page.php 文件(也可以是 archive.php 文件),它们位于您的模板目录中。在这些文件中找到以下代码:the_content() 和 the_title(),这两个函数实际上显示了数据库中的所有信息。

    所以把它们剪成这样: &lt;?php echo chop_string(the_title(), 5); ?&gt; 或 &lt;?php echo chop_string(the_content(), 5); ?&gt;

    【讨论】:

    • 我应该在哪里添加这个 . 也很遗憾地问你的例子中限制的文本是 bla bla bla 吗?
    • 是的,代码中的文字只是示例。将&lt;?php echo chop_string('your real text here', 5); ?&gt; 放置在页面模板中您要剪切文本的位置。
    • 但我在 wordpress 网站中。文本自动生成
    • 我实际要砍的文本在 $content 中。因此,如果我添加这样的代码 工作?或 $content = Chop_string($content, 5);然后 echo $content .please help!!
    【解决方案3】:

    不,CSS 是展示性的,因此不涉及特定内容的长度。它可以使用overflow 和text-overflow 属性剪辑诸如盒子大小之类的表示属性,但不基于内容。不过,您可以轻松地在服务器端修复它:

    $text = strlen($text) > 250 ? substr($text, 0, 250).'&hellip;' : $text;
    echo "<p>$text</p>";
    

    如果文本长于 250 个字符,则会在 250 个字符后插入一个省略号。

    【讨论】:

    • 有趣的是,真正回答问题的唯一答案(按字符/字数限制)在没有评论的情况下被直接否决,而所有其他答案却解决了另一个问题(受容器大小限制)。跨度>
    • 我在哪里可以添加这个?
    • 在生成内容的服务器端代码中。
    【解决方案4】:

    有两种方法可以实现:

    1) CSS方式

    添加css属性text-overflow:ellipsis

    .truncate {
      width: 250px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    

    2) 技巧(HTML)

    在div and add **maxlength=50** 或任何你需要的地方添加一个textarea 控件,使用css border:none 属性隐藏边框。

    【讨论】:

      【解决方案5】:

      借助 CSS3 属性“text-overflow” - 您可以设置框的宽度,从而得到所有 div 的确切宽度;

      文本不会在单词中间被剪切,它会在文本末尾添加“...”,提示它是预览文本;

      HTML 代码:

      <div id="myDiv">Just some text - you won't see ME!</div>
      

      CSS 代码:

      #myDiv {
          background: red;
          color: white;
          font-size: 20px;
          height: 50px;
          overflow: hidden;
          width: 256px;
          white-space:nowrap;
          text-overflow: ellipsis;
      }
      

      在FF、Chrome、IE11下测试

      【讨论】:

      • 这已在多个答案中提出。原始发布者不想将自己限制在一行巫婆“文本溢出:省略号”。
      【解决方案6】:

      这是我的解决方案演示:limit-printed-words.html

      它适用于任何嵌套级别甚至按钮内容。



      源项目:scriptbucket

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-06
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 2010-11-30
        • 2013-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多