js string生成dom节点

传入string生成dom节点

实现:

function genElement(text) {
	const parser = new DOMParser();	
	const doc = parser.parseFromString(text, 'text/html');	
	return document.adoptNode(doc.body.firstChild);
}

例子:

let dom = document.getElementById('dom');
dom.appendChild(genElement(`
        
august
champagne problems
exile
`
))

你可能感兴趣的:(js,javascript,前端,html)