使用 Javascript 更改 HTML 中的文本

问题描述 投票:0回答:4
javascript html
4个回答
0
投票

要更改 html 标签的内容,请使用 innerHTML 属性:

var h4 = document.getElementsByClassName("author_name")[0],
    target = h4.childNodes[0];
target.innerHTML = "About the author";

0
投票

这是代码:

document.getElementById("id-of-element").innerHTML = "new text";
<!DOCTYPE html>
<html>
<body>
<p id="id-of-element">This text will be replaced</p>
</body>
</html>


0
投票

这非常简单,您只需使用innerText或textcontent,如下例所示

//textConent Method
    const authorName = document.querySelectorAll('author_name').textContent="Text Here"
//innerText Method
    const authorName = document.querySelectorAll('author_name').innerText="Text Here"


-1
投票

如果您使用jQuery,您可以轻松更改。

$("h4.author_name a").text('关于作者');

您可以在这里找到详细的文章

© www.soinside.com 2019 - 2024. All rights reserved.