[英]Add text to <p> tag by using ID of body tag in javascript
I have a query. 我有一个问题。 I want to insert text to a ' Paragraph ' tag, but here ' Paragraph ' tag doesn't have any ID.
我想在' Paragraph '标签中插入文字,但这里' Paragraph '标签没有任何ID。 So, using ID of body tag I want to insert text.
所以,使用body标签的ID我想插入文本。 So, I am confused here, please help me here.
所以,我很困惑,请在这里帮助我。 Following are my html code
以下是我的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>
Now, I want to add text using javascript in p tag. 现在,我想在p标签中使用javascript添加文本。 So, please guide me here.
所以,请在这里指导我。 Thank you.
谢谢。
Edit: I want that text to come from another variable. 编辑:我希望该文本来自另一个变量。 So, please tell me is it possible using jquery?
那么,请告诉我是否有可能使用jquery?
JavaScript solution: To add text in the first p
tag. JavaScript解决方案:在第一个
p
标记中添加文本。
document.getElementsByTagName("p")[0].innerHTML="Whatever text!";
Snippet: 片段:
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>
Use Find Method to find Childs.. 使用查找方法查找孩子..
(function(){ $("#tinymce").find("p").html("asd"); })();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <body dir="ltr" id="tinymce" class="mceContentBody " contenteditable="true"> <p><br data-mce-bogus="1"></p> </body>
You can use immediate child selector >
along with body
selector target the <p>
element: 你可以使用直接子选择器
>
和body
选择器目标<p>
元素:
$("body > p").text("new text");
Additionally, if you have only one <p>
element in your whole HTML document then you can use the tagname selector directly: 此外,如果整个HTML文档中只有一个
<p>
元素,那么您可以直接使用标记名选择器:
$("p").text("new text");
试试这个
$("p").html("Write Your Text");
Try this 试试这个
$("body > p").text("YourText");
$("body > p").text("YourText");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.