我想在 paragraph 中插入一些文本 标签,但这里的段落标签没有 ID。因此,使用我想要插入文本的 body 标签的 ID。我该怎么做?
以下是我的 HTML 代码:
<html>
<head xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="X-UA-Compatible" content="IE=7">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body dir="ltr" id="tinymce" class="mceContentBody " contenteditable="true">
<p><br data-mce-bogus="1"></p>
</body>
</html>
现在,我想在 p 标签中使用 JavaScript 添加文本。
我希望该文本来自另一个变量。是否可以使用 jQuery ?
最佳答案
JavaScript 解决方案:
在第一个 p
标签中添加文本。
document.getElementsByTagName("p")[0].innerHTML="Whatever text!";
片段:
var list = document.getElementsByTagName("p")[0].innerHTML="Whatever text!";
//alert(list);
<html>
<head xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="X-UA-Compatible" content="IE=7">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body dir="ltr" id="tinymce" class="mceContentBody " contenteditable="true">
<p>
<br data-mce-bogus="1">
</p>
</body>
</html>
关于javascript - 在 JavaScript 中使用 body 标签的 ID 将文本添加到 <p> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28600453/