2016-07-24 100 views
1

我正在學習javascript和我的WordPress博客我正在使用它縮短真正的長期標題。Javascript - 縮短標題太長

<script> 
    jQuery(".wlt_search_results h4 a").each(function() { 
     if(jQuery(this).text().length > 30) { 
      jQuery(this).text(jQuery(this).text().substr(0,80)); 
      jQuery(this).append('…..View More'); 
     } 
    }); 
</script> 

是否有可能延長所以如果這個標題少於30個字符,但默認情況下並不顯示....查看更多?我不確定如何實現這一點,所以任何指針都會非常適用。

謝謝! :)

+0

可能是有趣的調查[CSS省略號(https://css-tricks.com/代碼段/ CSS /截斷串與 - 省略號/)。 – JCOC611

+0

jQuery(this).append('... ..查看更多');刪除這一行 –

回答

1

使用此代碼替換代碼

if(jQuery(this).text().length > 30){ 
    jQuery(this).text(jQuery(this).text().substr(0,30)+'...View More'); 
} 

我的結果

enter image description here