如何使用jQuery更改文本 [英] How to change a text with jQuery
本文介绍了如何使用jQuery更改文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个动态创建的ID为toptitle
的h1
,但是我无法更改HTML.
根据页面的不同,标题也会有所不同.现在,当它是Profile时,我想使用jQuery将其更改为New word
.
I have an h1
with id of toptitle
that is dynamically created, and I am not able to change the HTML.
It will have a different title depends on a page. Now when it is Profile, I want to change it to New word
with jQuery.
<h1 id="toptitle">Profile</h1> // Changing only when it is Profile
// to
<h1 id="toptitle">New word</h1>
注意:如果文本为Profile
,则将其更改为New word
.
Note: If the text is Profile
, then change it to New word
.
推荐答案
这种方法应该可以解决问题:
Something like this should do the trick:
$(document).ready(function() {
$('#toptitle').text(function(i, oldText) {
return oldText === 'Profil' ? 'New word' : oldText;
});
});
仅当内容为Profil
时,才替换内容.请参阅jQuery API中的 text
.
This only replaces the content when it is Profil
. See text
in the jQuery API.
这篇关于如何使用jQuery更改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文