jQuery 如何更改元素内的文本值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7961416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:32:11  来源:igfitidea点击:

how to change text value within an element?

jqueryreplace

提问by Yusaf Khaliq

How do you change the text for all within the ato

你如何更改所有在ato 中的文本

continue reading

继续阅读

using jquery

使用jQuery

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

回答by alex

Do it with jQuery inside of a document ready handler ($(fn))...

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

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

jsFiddle.

js小提琴

For the sake of it, here is how to do it without jQuery....

为此,这里是如何在没有 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.

js小提琴

This will work good for your piece of HTML, but it isn't too generic.

这对您的 HTML 片段很有用,但它不是太通用。

If you don't care about setting innerTextproperty, you could use...

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

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

I wouldn't recommend it though.

不过我不会推荐它。

回答by Clive

This should do it:

这应该这样做:

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

回答by Dogbert

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

回答by Hady Elsahar

Through the text property in jQuery that changes the inner text of the <tag>

通过 jQuery 中的 text 属性改变内部文本 <tag>

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