【JavaScript】用字符串生成 DOM 元素

代码如下:

function createElementFromHTML(htmlString) {
  var div = document.createElement('div');
  div.innerHTML = htmlString.trim();

  // Change this to div.childNodes to support multiple top-level nodes.
  return div.firstChild;
}

来源:Creating a new DOM element from an HTML string using built-in DOM methods or Prototype

你可能感兴趣的:(Javascript,javascript)