如何更改元素内的文本值? [英] how to change text value within an element?
本文介绍了如何更改元素内的文本值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将a
中所有内容的文本更改为
How do you change the text for all within the a
to
继续阅读
使用jquery
<div class="post-read">
<a href="http://www.google.com">Read More</a>
</div>
推荐答案
使用jQuery在文档就绪处理程序($(fn)
)中完成此操作...
Do it with jQuery inside of a document ready handler ($(fn)
)...
$('.post-read a').text('continue reading');
jsFiddle .
为此,这里是在没有jQuery的情况下如何做的....
For the sake of it, here is how to do it without 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很好,但不是太通用.
This will work good for your piece of HTML, but it isn't too generic.
如果您不关心设置innerText
属性,则可以使用...
If you don't care about setting innerText
property, you could use...
anchor.textContent = anchor.innerText = 'continue reading';
我不会推荐它.
这篇关于如何更改元素内的文本值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文