修改文本javascript

modify a text javascript

本文关键字:javascript 文本 修改      更新时间:2023-09-26

如何为我想在javascript中修改的文本设置ID。

Example:
<hr>
<b>Text:</b>
This is my text to modify.
<hr>
<b>Next Text:</b>
AAA
....

我想修改文本"这是我的文本修改。"在javascript。我需要类似于下面的代码,但是我如何为我需要修改的文本设置id ?

document.getElementById('?').innerHTML="modified text";

谢谢你的帮助

这样做:

<span id="modify">This is the text to modify</span>

然后在JavaScript中:

document.getElementById('modify').innerHTML="New text ";

注意:除了<span>,您还可以使用<p>, <h1> - <h6>或任何其他元素

你必须在html标签ie:

中包装文本
 <span id="textToModify">Some text</span>

之后可以

document.getElementById('textToModify').innerHTML="modified text";

将"This is my text to modify."换行到一个元素中,然后修改

 <p id="text-to-modify">This is my text to modify</p>
 document.getElementById('text-to-modify').innerHTML="modified text";

按照您的代码,执行如下操作

Example:
<hr>
<b>Text:</b>
<span id="id1">This is my text to modify.</span>
<hr>
<b>Next Text:</b>
AAA
....

JS像这样

document.getElementById('id1').innerHTML="modified text";

HTML:

Example:
<hr>
<b>Text:</b>
<a id="myTxt">This is my text to modify.</a>
<hr>
<b>Next Text:</b>
AAA
....

和JS:

document.getElementById("myTxt").innerHTML = "Modified!";