使用 javascript 中的 body 标签 ID 将文本添加到 <p> 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28600453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:04:17  来源:igfitidea点击:

Add text to <p> tag by using ID of body tag in javascript

javascripthtml

提问by santa banta

I have a query. I want to insert text to a 'Paragraph' tag, but here 'Paragraph' tag doesn't have any ID. So, using ID of body tag I want to insert text. So, I am confused here, please help me here. Following are my html code

我有一个疑问。我想在“段落”标签中插入文本,但这里的“段落”标签没有任何 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>

Now, I want to add text using javascript in p tag. So, please guide me here. Thank you.

现在,我想在 p 标签中使用 javascript 添加文本。所以,请在这里指导我。谢谢你。

Edit: I want that text to come from another variable. So, please tell me is it possible using jquery?

编辑:我希望该文本来自另一个变量。那么,请告诉我可以使用jquery吗?

回答by Ani Menon

JavaScript solution:To add text in the first ptag.

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>

回答by Dhaval

Use FindMethod to find Childs..

使用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>

回答by Milind Anantwar

You can use immediate child selector >along with bodyselector 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:

此外,如果<p>整个 HTML 文档中只有一个元素,则可以直接使用标记名选择器:

 $("p").text("new text");

回答by Imtiaz Pabel

Try this

尝试这个

$("p").html("Write Your Text");

回答by Amy

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>