vtils
Version:
一个面向业务的 JavaScript/TypeScript 实用程序库。
25 lines (23 loc) • 591 B
JavaScript
// https://github.com/fregante/doma/blob/master/index.ts
/**
* 将 HTML 字符串转为 DocumentFragment。
*
* @param html HTML 字符串
*/
export function htmlToDocumentFragment(html) {
if (html == null) {
return new DocumentFragment();
}
var template = document.createElement('template');
template.innerHTML = html;
return template.content;
}
/**
* 将 HTML 字符串转为 Element。
*
* @param html HTML 字符串
*/
export function htmlToElement(html) {
var _ref;
return (_ref = htmlToDocumentFragment(html).firstElementChild) != null ? _ref : undefined;
}