javascript 使用 jQuery 添加一些文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11766432/
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-10-26 14:16:55  来源:igfitidea点击:

Add some text using jQuery

javascriptjquery

提问by GibboK

I'm using jQuery, I have a div with H2 tags

我正在使用 jQuery,我有一个带有 H2 标签的 div

<div id="preload"><h2></h2></div>

I would like to know how to use jQuery to add a text within the <h2> tags

我想知道如何使用 jQuery 在 <h2> tags

Final result should be:

最终结果应该是:

<div id="preload"><h2>Some text here</h2></div>

I need to add the text without replacing the tags <h2></h2>in the jQuery script.

我需要添加文本而不替换<h2></h2>jQuery 脚本中的标签。

Any ideas? Many thanks!

有任何想法吗?非常感谢!

回答by Ashutosh Jindal

Try something like this :

尝试这样的事情:

$("div#preload h2").html("Some Text Here")

See the above code in action for your sample HTML here

此处查看示例 HTML 的上述代码

回答by Vinit

$("#preload h2").text('WOOt Woot');

回答by Austin

Use .text

利用 .text

$("#preload h2").text("Some text here");

Also not that this must be within either $(document).ready(function () { ... })or $(function () { ... }). I would use the latter as it saves typing.

也不是这必须在$(document).ready(function () { ... })or 或 内$(function () { ... })。我会使用后者,因为它可以节省打字时间。

$(function () {
    $("#preload h2").text("Some text here");
});

回答by j08691

$('#preload h2').html('foo');

OR

或者

$('#preload h2').text('foo');