如何动态替换html文本? [英] How can I replace html text dynamically?
问题描述
我正在添加文字I Tot I Taw a Puddy Tat!以jQuery这样的方式在一个页面顶部的准备功能:
I'm adding the text "I Tot I Taw a Puddy Tat!" to the top of a page with jQuery this way in the ready function:
$("#Twitterati").html("<h3>I Tot I Taw a Puddy Tat!</h3>").append(tweetiePie);
...但我想动态替换占位符文本(I Tot I Taw a Puddy Tat !)与此文本输入控件的内容:
...but I want to dynamically replace that placeholder text ("I Tot I Taw a Puddy Tat!") with the contents of this text input control:
<input type="text" name="inputQuery" id="inputQuery" placeholder="Enter something" style="display: inline-block;" />
...当Enter键被捣碎时。我如何做?
...when the Enter key is mashed. How can I do that?
我尝试过:
$("#inputQuery").keyup(function(e) {
if(e.keyCode == 13) {
$("#Twitterati h3").val($("#inputQuery".val()));
}
});
...这样:
$("#inputQuery").keyup(function(e) {
if(e.keyCode == 13) {
$("#Twitterati h3").replaceWith($("#inputQuery".val()));
}
});
...但没有人做任何事,我可以看到...
...but neither one did anything, that I could see...
现在工作得很好,但是,当inputQuery val替换文本/标题/标题时,我希望tweet可以刷新。 IOW,我可以将文本从jquery更改为html5,但是我显示的tweets仍然是jquery的推文。如何获得该div刷新?
This is working pretty well, now; but I want the tweets to refresh when the inputQuery val replaces the text/caption/heading, too. IOW, I can change the text from, say, "jquery" to "html5" but the tweets I've got displaying remain the jquery tweets. How can I get that div to refresh?
推荐答案
尝试这样:
$("#inputQuery").keyup(function(e) {
if(e.keyCode == 13) {
$("#Twitterati h3").html($(this).val());
}
});
在这里缺少一个关闭括号 $(#inputQuery< -
,最后一个额外的括号 .val())); < ----
Missing a closing bracket here $("#inputQuery" <----
and a extra bracket at last .val())); <----
这篇关于如何动态替换html文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!