html文本溢出顯示省略字符的兩種經常使用方法

方法一:使用CSS溢出省略的方式解決css

解決效果以下:html

css代碼:web

            display: -webkit-box;
            display: -moz-box;
            white-space: pre-wrap;
            word-wrap: break-word;
            overflow: hidden;
            text-overflow: ellipsis;
            -webkit-box-orient: vertical;
            -webkit-line-clamp:2;   /*顯示行數*/

 

方法二:使用jQuery截取替換文本內容的方式解決this

解決效果以下:spa

 js代碼:3d

    $(".text").each(function() {
        if ($(this).text().length > 20) {//要求字數大於20才進行字數替換
            $(this).html($(this).text().replace(/\s+/g, "").substr(0, 80) + "...")
            //從0到80開始替換,將剩餘文本內容替換爲"..."
        }
    })

上述兩種方法做用在文本內容的類名便可。code

相關文章
相關標籤/搜索