代码之家  ›  专栏  ›  技术社区  ›  Liam neesan

如何使用JQuery删除并向span标记添加新文本?

  •  0
  • Liam neesan  · 技术社区  · 6 年前

    span

    html代码

    <span class="mobsize-showmore-text">
            Show More
            <button class="mdl-button mdl-js-button mdl-button--fab mdl-button--mini-fab mobsize-btn-downarrow">
            <i class="material-icons down-icon">keyboard_arrow_down</i>
        </button>
    </span>
    

    滑动分页

    $(".mobsize-btn-downarrow").click(function () {
        $(".mobsize-div-toggle").slideToggle('fast');
        alert("mobsize-showmore-text = " + $('.mobsize-showmore-text').clone()
                                   .children()
                                   .remove()
                                   .end()
                                   .text());
    
        $('.mobsize-showmore-text').clone()
                                   .children()
                                   .remove()
                                   .end()
                                   .text('show less');
    
    });
    

    显示更多 显示较少

    1 回复  |  直到 6 年前
        1
  •  3
  •   David    6 年前

    我不知道这些电话 .clone() .remove() , .end() 克隆

    简化。

    只需将要修改的文本包装到它自己的元素中,并将其作为目标:

    <span class="mobsize-showmore-text">
        <span class="showmore-text-content">Show More</span>
        <button class="mdl-button mdl-js-button mdl-button--fab mdl-button--mini-fab mobsize-btn-downarrow">
            <i class="material-icons down-icon">keyboard_arrow_down</i>
        </button>
    </span>
    
    $(".mobsize-btn-downarrow").click(function () {
        $(".mobsize-div-toggle").slideToggle('fast');
        $('.mobsize-showmore-text .showmore-text-content').text('Show Less');
    });