我正在使用jQuery,我有一个带有H2标签的div
<div id="preload"><h2></h2></div>
Run Code Online (Sandbox Code Playgroud)
我想知道如何使用jQuery在其中添加文本 <h2> tags
最终结果应该是:
<div id="preload"><h2>Some text here</h2></div>
Run Code Online (Sandbox Code Playgroud)
我需要添加文本而不替换<h2></h2>jQuery脚本中的标记.
有任何想法吗?非常感谢!
Ash*_*dal 10
尝试这样的事情:
$("div#preload h2").html("Some Text Here")
Run Code Online (Sandbox Code Playgroud)
请在此处查看上面的代码HTML代码
使用 .text
$("#preload h2").text("Some text here");
Run Code Online (Sandbox Code Playgroud)
同样不是必须在$(document).ready(function () { ... })或中$(function () { ... })。我会使用后者,因为它可以节省键入内容。
$(function () {
$("#preload h2").text("Some text here");
});
Run Code Online (Sandbox Code Playgroud)