简体   繁体   English

使用本机 javaScript 附加 html

[英]Appending html using native javaScript

I need to append some html to an existing element using pure javaScript:我需要 append 一些 html 到使用纯 javaScript 的现有元素:

function create(htmlStr) {
  var frag = document.createDocumentFragment(),
    temp = document.createElement('div');
  temp.innerHTML = htmlStr;
  while (temp.firstChild) {
    frag.appendChild(temp.firstChild);
  }
  return frag;
}

var target = document.querySelectorAll(".container-right");
var fragment = create(
  '<div class="freetext"><p>Some text that should be appended...</p></div>'
);
document.body.insertBefore(fragment, document.body.childNodes[0]);

It's kind of working, but I have two questions:这有点工作,但我有两个问题:

  1. How can I make sure that the html fragment is appended to the div with the class container-right and not just the body?如何确保 html 片段被附加到 div 与 class container-right ,而不仅仅是身体? Changing the last line to document.body.insertBefore(fragment, target);将最后一行更改为document.body.insertBefore(fragment, target); doesn't work.不起作用。

  2. How can I insert the html after the content in the target element - after the existing content - like jQuery's append()?如何在目标元素中的内容之后插入 html - 在现有内容之后 - 如 jQuery 的 append()?

Any help is much appreciated.任何帮助深表感谢。

JsFiddle here . JsFiddle 在这里

Well, I know this works: 嗯,我知道这有效:

let elem = document.querySelector ( 'css-selector (id or class)' )

That should give you your element. 这应该给你你的元素。 Then you do this: 然后你这样做:

elem.innerHTML = elem.innerHTML + myNewStuff;

That'll append your html to the innerHTML of the element. 这会将你的html追加到元素的innerHTML中。 I tried it quickly, it works. 我快速尝试了,它有效。

var target = document.querySelector(".container-right");

var p = document.createElement('p');
p.innerHTML = "Some text that should be appended...";

var div = document.createElement('div');
div.appendChild(p);

var fragment = document.createDocumentFragment();
fragment.appendChild(div);

target.appendChild(fragment);

JSFiddle 的jsfiddle

Based on this answer to a similar question, I have found that insertAdjacentHTML is a good fit for this kind of problems.基于对类似问题回答,我发现insertAdjacentHTML非常适合此类问题。 I haven't tested it on a Node List, but with a single node it works perfectly.我没有在节点列表上测试过它,但是使用单个节点它可以完美地工作。

insertAdjacentHTML has a great browser compatibility (back to IE4), plus it lets you decide where you want to insert the HTML (see here ). insertAdjacentHTML具有出色的浏览器兼容性(回到 IE4),此外,它还可以让您决定要在哪里插入 HTML(请参见此处)。

var target = document.querySelector(".container-right");
var newContent = '<div class="freetext"><p>Some text that should be appended...</p></div>';
target.insertAdjacentHTML('beforeend', newContent);

Try this: 试试这个:

var target = document.querySelector(".container-right");
target.innerHTML += '<div class="freetext"><p>Some text that should be appended...</p></div>';

Giorgio Tempesta, best answer.乔治·坦佩斯塔,最佳答案。 with this function(insertAdjacentHTML) there is no data loss if you are manipulating input fields.使用此功能(插入AdjacentHTML),如果您正在操作输入字段,则不会丢失数据。

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

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