Javascript - 缩短过长的标题

时间:2016-07-24 21:03:21

标签: javascript wordpress

我正在学习javascript,在我的wordpress博客上,我使用它缩短了很长的标题。

<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个字符,则无法显示....默认情况下查看更多?我不确定如何实现这一点,所以任何指针都会非常有用。

谢谢! :)

1 个答案:

答案 0 :(得分:1)

使用此代码替换您的代码

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

我的结果

enter image description here