-
检测 DOM 元素是不是其他 DOM 元素的后代.
// jQuery
$.contains(el, child);
// Native
el !== child && el.contains(child);
-
全局执行 JavaScript 代码。
// jQuery
$.globaleval(code);
// Native
function Globaleval(code) {
const script = document.createElement('script');
script.text = code;
document.head.appendChild(script).parentNode.removeChild(script);
}
// Use eval, but context of eval is current, context of $.Globaleval is global.
eval(code);
-
- parseHTML
// jQuery
$.parseHTML(htmlString);
// Native
function parseHTML(string) {
const context = document.implementation.createHTMLDocument();
// Set the base href for the created document so any parsed elements with URLs
// are based on the document's URL
const base = context.createElement('base');
base.href = document.location.href;
context.head.appendChild(base);
context.body.innerHTML = string;
return context.body.children;
}
- parseJSON
传入格式正确的 JSON 字符串并返回 JavaScript 值.
// jQuery
$.parseJSON(str);
// Native
JSON.parse(str);