简体   繁体   English

在HTML元素之前或之后添加文本

[英]Add text before or after an HTML element

If i have an HTML element like <div> with some text inside or another elements can I add before or after this div some text data without an html element, just plain text? 如果我有一个像<div>这样的HTML元素,里面有一些文本,或者我可以在这个div之前或之后添加一些没有html元素的文本数据,只是纯文本?

I'd like to use only pure Javascript. 我只想使用纯Javascript。

Something like : 就像是 :

<div id="parentDiv">
   my text must be added here
  <div id="childDiv"></div>
</div>

Yes, you can create a text node with document.createTextNode('the text') 是的,您可以使用document.createTextNode('the text')创建一个文本节点

Then you can insert it like an element, with appendChild or insertBefore. 然后你可以像一个元素一样插入它,使用appendChild或insertBefore。

Example that insert a text before #childDiv: 在#childDiv之前插入文本的示例:

var text = document.createTextNode('the text');
var child = document.getElementById('childDiv');
child.parentNode.insertBefore(text, child);

Just for the record: 仅供记录:

div.insertAdjacentHTML( 'beforeBegin', yourText );

where div is your child-DIV. div是你的孩子-DIV。

Live demo: http://jsfiddle.net/ZkzDk/ 现场演示: http //jsfiddle.net/ZkzDk/

In regards to the topic and the users question for inserting before or after , here is an example for after: 关于主题和用户之前或之后插入的问题,以下是以下示例:

   var text = document.createTextNode("my text must be added here.");
   var childTag = document.getElementById("childDiv");

   childTag.parentNode.insertBefore(text, childTag.nextSibling);

If the childTag does not have any siblings, it is okay because the insertBefore method handles this case and simply adds it as the last child. 如果childTag没有任何兄弟,那就没关系,因为insertBefore方法处理这种情况并简单地将它添加为最后一个子节点。

Also can possibly use the appendChild() method after creating text node then add your childTag via the parentNode. 也可以在创建文本节点后使用appendChild()方法,然后通过parentNode添加childTag。

If you just need text, I find that element.insertAdjacentText(position, text) is flexible for many scenarios and is also compatible with older browsers like IE6. 如果您只需要文本,我发现element.insertAdjacentText(position, text)在许多场景中都很灵活,并且与IE6等旧版浏览器兼容。 Where position is exactly where you want the text to be and text is the text node or just a string. position恰好是文本所在的positiontext是文本节点或只是字符串。 The options are: 选项是:

  • 'beforebegin' Before the element itself. 'beforebegin'在元素本身之前。
  • 'afterbegin' Just inside the element, before its first child. 'afterbegin'就在元素里面,在它的第一个孩子之前。
  • 'beforeend' Just inside the element, after its last child. 'beforeend'就在元素里面,在它的最后一个孩子之后。
  • 'afterend' After the element itself. 'afterend'元素本身之后。

Like this: 像这样:

let div = document.getElementById('parentDiv');
div.insertAdjacentText('afterbegin', 'My Plain Text..');

You can add text node. 您可以添加文本节点。 Create node - document.createTextNode('text') and then insert/append/replace - do whatever you want. 创建节点 - document.createTextNode('text')然后插入/追加/替换 - 做你想做的任何事情。

Something like this should do it: 这样的事情应该这样做:

<script type="text/javascript">
  var parent = document.getElementById('parentDiv');
  var sibling = document.getElementById('childDiv');
  var text = document.createTextNode('new text');
  parent.insertBefore(text, sibling);
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM