缩短每个第二段中的文字

时间:2014-07-10 21:24:48

标签: jquery html css wordpress

我正在为Wordpress网站写一个Jquery。改变html是不可能只有CSS或Jquery的问题,所以如果有一个解决方案来解决我的问题,那就太棒了。

我的网站上有几个div:

<div class="wf-cell wf-span-4 wpb_column column_container ">
    <div class="wpb_text_column wpb_content_element textblock_grey">
        <div class="wpb_wrapper">
            <h4> header here </h4>
            <p> image with link here </p>
            <p> TEXT HERE</p>
            <p> link here </p>
        </div>
    </div>
</div>

我不想缩短textblock_grey div中第二个p元素中的文本。我已经尝试过写作了:

if ($(".textblock_grey p:nth-child(2)").text().length >60) {
    $(".textblock_grey p:nth-child(2)").text().substring(0, 60);
}

但它不起作用我怀疑它只测量我网站上所有div中所有p元素的长度,而不是逐个测量每个div ...

1 个答案:

答案 0 :(得分:3)

你从未真正设置过文字:

$(".textblock_grey p:nth-child(2)").text(function() {
    return $(this).text().substring(0, 60);
});
相关问题