【问题标题】:how to change text value within an element?如何更改元素内的文本值?
【发布时间】:2011-12-19 04:07:09
【问题描述】:

如何将a 中的所有文本更改为

继续阅读

使用 jquery

<div class="post-read">
<a href="http://www.google.com">Read More</a>
</div>

【问题讨论】:

    标签: jquery replace


    【解决方案1】:

    应该这样做:

    $('.post-read a').html('continue reading');
    

    【讨论】:

      【解决方案2】:
      $('.post-read a').html("continue reading")
      

      【讨论】:

        【解决方案3】:

        通过 jQuery 中的 text 属性更改 &lt;tag&gt; 的内部文本

        $(document).ready(function () {
            $('.postread a').text("your new text here ");
        }
        

        【讨论】:

        • 你只需要 $(document).ready(function () { } 因为在整个文档加载之前你不能选择任何 html 标签
        • jQuery 没有innerText() 方法。
        【解决方案4】:

        在文档就绪处理程序 ($(fn)) 中使用 jQuery...

        $('.post-read a').text('continue reading');
        

        jsFiddle.

        为了它,这里是如何在没有 jQuery 的情况下做到这一点......

        var anchor = document.getElementsByClassName('post-read')[0].getElementsByTagName('a')[0],
            textProperty;
        
        if (anchor.textContent) {
            textProperty = 'textContent';
        } else if (anchor.innerText) {
            textProperty = 'innerText';
        }
        anchor[textProperty] = 'continue reading';
        

        jsFiddle.

        这对你的 HTML 很有效,但它不是太通用。

        如果您不关心设置innerText 属性,您可以使用...

        anchor.textContent = anchor.innerText = 'continue reading';
        

        我不推荐它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-04-14
          • 2014-05-25
          • 1970-01-01
          • 1970-01-01
          • 2020-09-18
          • 2011-05-05
          • 1970-01-01
          相关资源
          最近更新 更多