• 包含

    检测 DOM 元素是不是其他 DOM 元素的后代.

    1. // jQuery
    2. $.contains(el, child);
    3. // Native
    4. el !== child && el.contains(child);
  • Globaleval

    全局执行 JavaScript 代码。

    1. // jQuery
    2. $.globaleval(code);
    3. // Native
    4. function Globaleval(code) {
    5. const script = document.createElement('script');
    6. script.text = code;
    7. document.head.appendChild(script).parentNode.removeChild(script);
    8. }
    9. // Use eval, but context of eval is current, context of $.Globaleval is global.
    10. eval(code);
  • 解析

    • parseHTML
    1. // jQuery
    2. $.parseHTML(htmlString);
    3. // Native
    4. function parseHTML(string) {
    5. const context = document.implementation.createHTMLDocument();
    6. // Set the base href for the created document so any parsed elements with URLs
    7. // are based on the document's URL
    8. const base = context.createElement('base');
    9. base.href = document.location.href;
    10. context.head.appendChild(base);
    11. context.body.innerHTML = string;
    12. return context.body.children;
    13. }
    • parseJSON

    传入格式正确的 JSON 字符串并返回 JavaScript 值.

    1. // jQuery
    2. $.parseJSON(str);
    3. // Native
    4. JSON.parse(str);