如何使用 javascript 在下面代码中的位置插入一些文本?感谢任何帮助,因为我仍然是 javascript 的新手。
<div class = "post-body entry-content">
<div class = "auto-thumbnail">
<table width = "500px" height = "auto" border = "0" align = "center">
<tbody>
<tr><td>
<a href = "http://www.google.com" target = "_blank"><img src = "example.jpg" border = "0"/></a><br /><br />
Author : Jason<br />
Book Title : Example Book<br />
Release Date : 2013.05.11<br /><br />
Description : <br /><br />
Here is where I want to add some text<br /><br /><!--Add text on this line-->
Hello, example....<br />
<br />
</td></tr>
</tbody>
</table>
</div>
</div>
最佳答案
放一些<p>
文本周围的标签,带有 id
属性,然后使用 document.getElementById()
更改文本:
<p id = "textline">I want to add some text here</p><br /><br />
<script type = "text/javascript">
function changeText()
{
document.getElementById("textline").innerHTML = "changed text";
}
</script>
当您想要更改文本时,请记住调用该函数。如果能更好地描述您想要做什么以及为什么想要这样做就更好了。
编辑:
<p class = "textline">I want to add some text here</p><br /><br /><!--script will affect both lines-->
<p class = "textline">I want to add some text here</p><br /><br />
<script type = "text/javascript">
function changeText()
{
document.getElementsByClassName("textline").innerHTML = "changed text";
}
</script>
编辑2:
if(document.getElementById("your_id")!=null)
{
document.getElementById("your_id").innerHTML = "changed text";
}
else
{
alert("Element does not exist");
}
关于javascript - 如何使用 JavaScript 添加一些文本来发布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16494645/