{"id":6443,"date":"2017-09-28T01:07:48","date_gmt":"2017-09-27T17:07:48","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=6443"},"modified":"2018-07-24T11:05:08","modified_gmt":"2018-07-24T03:05:08","slug":"js-dom-before-after-replacewith-append-prepend","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2017\/09\/js-dom-before-after-replacewith-append-prepend\/","title":{"rendered":"before(),after(),prepend(),append()\u7b49\u65b0DOM\u65b9\u6cd5\u7b80\u4ecb"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=6443\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=6443<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u5f97\u5230\u539f\u4f5c\u8005\u4e66\u9762\u8bb8\u53ef\uff0c\u540c\u65f6\u4fdd\u7559\u539f\u4f5c\u8005\u548c\u51fa\u5904\uff0c\u6458\u8981\u5f15\u6d41\u5219\u968f\u610f\u3002<\/p>\n<h3>\u4e00\u3001DOM API\u4e5f\u5728\u4e0d\u65ad\u5347\u7ea7<\/h3>\n<p><img src='http:\/\/image.zhangxinxu.com\/image\/blog\/201709\/2017-09-26_232426.png' width=\"616\" height=\"122\" alt=\"DOM\u6807\u51c6\u4ee5\u53ca\u66f4\u65b0\" class=\"alignnone\" \/><\/p>\n<p>web\u524d\u7aef\u6807\u51c6\u4e00\u76f4\u5728\u4e0d\u65ad\u5347\u7ea7\uff0c\u6bd4\u65b9\u8bf4\uff0c\u8bf4\u4e86\u5f88\u591a\u5e74\u7684HTML5\u3001CSS3\uff0c\u4ee5\u53ca\u5929\u5929\u89c1\u7684ES6\u3002<\/p>\n<p>\u7136\u540e\uff0c\u4f3c\u4e4e\u5c31\u6ca1\u6709\u7136\u540e\u4e86\u3002\u5b9e\u9645\u4e0a\uff0c\u9664\u4e86HTML5\/CSS3\/ES6+\uff0c\u5173\u4e8eDOM\u6807\u51c6\u4e5f\u662f\u5728\u4e0d\u65ad\u5347\u7ea7\u8fdb\u6b65\uff0c\u800c\u4e14\u6d4f\u89c8\u5668\u4e5f\u5728\u6084\u6084\u5730\u8fdb\u884c\u8ddf\u8fdb\u4e0e\u652f\u6301\u3002<\/p>\n<p>\u7136\u800c\uff0c\u8fd9\u79cd\u8ddf\u8fdb\u4e0e\u652f\u6301\u5462\u975e\u5e38\u7684\u4f4e\u8c03\u4e0e\u9690\u8bb3\uff1b\u52a0\u4e0a\u884c\u4e1a\u7684\u8bdd\u8bed\u6743\u5927\u90e8\u5206\u96c6\u4e2d\u5728JS\u5de5\u7a0b\u5e08\u8eab\u4e0a\uff0c\u540c\u65f6DOM\u8fd9\u79cd\u4e1c\u897f\u6bd4\u8f83\u201c\u4f4e\u7ea7\u201d\u6491\u4e0d\u8d77\u573a\u9762\uff0c\u56e0\u6b64\u5404\u79cd\u4ec0\u4e48\u6280\u672f\u5927\u4f1a\u554a\uff0c\u5206\u4eab\u4f1a\u8bae\u554a\u7edd\u5bf9\u4e0d\u4f1a\u8bb2\u8fd9\u73a9\u610f\u513f\u7684\u3002<\/p>\n<p>\u4e8e\u662f\u90fd\u53eb\u4ed6\u6ce8\u610f\u529b\u5f88\u5bb9\u6613\u88ab\u5e26\u8d70\uff0c\u800c\u6ca1\u6709\u610f\u8bc6\u5230\u5728DOM\u64cd\u4f5c\u8fd9\u4e00\u5757\uff0c\u73b0\u5728\u5b9e\u9645\u4e0a\u5df2\u7ecf\u975e\u5e38\u5389\u5bb3\u4e86\u3002<\/p>\n<h3>\u4e8c\u3001before(),after(),prepend(),append()\u7b49\u65b0\u589eDOM API<\/h3>\n<p>\u63a5\u4e0b\u6765\u8981\u4ecb\u7ecd\u8fd9\u4e9b\u65b0\u589e\u7684DOM API\u65b9\u6cd5\uff0c\u90fd\u6bd4\u8f83\u65b0\uff0c\u5176\u8bbe\u8ba1\u76ee\u7684\u662f\u53ef\u4ee5\u50cfjQuery\u90a3\u6837\uff0c\u4f7f\u7528\u975e\u5e38\u7b80\u5355\u7684api\uff0c\u4fbf\u5229\u7684\u64cd\u4f5cdom\u5143\u7d20\u3002<\/p>\n<p>\u8fd9\u4e9bapi\u5305\u62ec\uff1a<code>before()<\/code>\uff0c<code>after()<\/code>\uff0c<code>replaceWith()<\/code>\uff0c<code>append()<\/code>\uff0c<code>prepend()<\/code>\uff0c\u597d\uff0c\u4e0b\u9762\u4e00\u4e2a\u4e00\u4e2a\u4ecb\u7ecd\u3002<\/p>\n<h4>1. DOM API\u4e4bbefore()<\/h4>\n<p>\u8fd9\u91cc\u7684<code>before()<\/code>\u662f\u4e2a<code>ChildNode<\/code>\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u8282\u70b9\u65b9\u6cd5\u3002\u8282\u70b9\u65b9\u6cd5\u5bf9\u5e94\u4e8e\u5143\u7d20\u65b9\u6cd5\u3002\u533a\u522b\u5728\u4e8e\uff0c\u8282\u70b9\u53ef\u4ee5\u76f4\u63a5\u662f\u6587\u672c\u8282\u70b9\uff0c\u751a\u81f3\u6ce8\u91ca\u7b49\u3002\u4f46\u662f\uff0c\u5143\u7d20\u5fc5\u987b\u8981\u6709HTML\u6807\u7b7e\u3002<\/p>\n<p>\u56e0\u6b64\uff0c<code>before()<\/code>\u7684\u53c2\u6570\u65e2\u53ef\u4ee5\u662fDOM\u5143\u7d20\uff0c\u4e5f\u53ef\u4ee5\u662fDOM\u8282\u70b9\uff0c\u751a\u81f3\u53ef\u4ee5\u76f4\u63a5\u5b57\u7b26\u5185\u5bb9\uff0c\u54a6\uff0c\u611f\u89c9\u6d3b\u8131\u8131\u7684jQuery\u7684<code>before()<\/code> API\u561b\uff1f\u6ca1\u9519\uff0c\u771f\u7684\u5f88\u7c7b\u4f3c\u4f3c\uff0c\u5e76\u4e14\u8bed\u4e49\u4e5f\u662f\u4e00\u6837\u7684\uff0c\u8868\u793a\u5f53\u524d\u8282\u70b9\u7684\u524d\u9762\u662fXXX\u3002<\/p>\n<p>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>void ChildNode.before((\u8282\u70b9\u6216\u5b57\u7b26\u4e32)... \u5176\u5b83\u66f4\u591a\u8282\u70b9);<\/pre>\n<p>\u4ece\u8bed\u6cd5\u53ef\u4ee5\u770b\u51fabefore()\u65b9\u6cd5\u652f\u6301\u591a\u4e2a\u53c2\u6570\uff0c\u5e76\u4e14\u53c2\u6570\u53ef\u4ee5\u662f\u8282\u70b9\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u5bf9\u8c61\u3002<\/p>\n<p>\u81f3\u4e8e\u5177\u4f53\u7ec6\u8282\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u4e00\u4e2a\u4f8b\u5b50\u6765\u5b9e\u9645\u9a8c\u8bc1\u3002<\/p>\n<p>\u5148\u6765\u770b\u4e00\u4e2a\u6700\u7b80\u5355\u4f8b\u5b50\uff0c\u5df2\u77e5HTML\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;img id=\"img\" src=\"mm0.jpg\"&gt;<\/pre>\n<p>\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5728\u56fe\u7247<code>&lt;img&gt;<\/code>\u524d\u9762\u63d2\u5165\u5199\u6587\u5b57\uff0c\u76f4\u63a5\u53ef\u4ee5\uff1a<\/p>\n<pre>document.getElementById('img').before('\u7f8e\u5973\uff1a');<\/pre>\n<p>\u6548\u679c\u4f1a\u662f\u8fd9\u6837\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201709\/2017-09-27_223359.png\" width=\"192\" height=\"173\" alt=\"\u56fe\u7247\u524d\u9762before()\u7f8e\u5973\u6587\u5b57\" class=\"alignnone\" \/><\/p>\n<p>\u5982\u679c\u6211\u4eec\u63d2\u5165\u7684\u662f\u4e00\u6bb5HTML\u5b57\u7b26\u4e32\uff0c\u90a3\u6548\u679c\u53c8\u662f\u600e\u6837\u7684\u5462\uff1f\u5982\u4e0b\uff1a<\/p>\n<pre>document.getElementById('img').before('&lt;strong&gt;\u7f8e\u5973\uff1a&lt;\/strong&gt;');<\/pre>\n<p>\u7ed3\u679c\uff0c\u5f53\u5f53\u5f53\u5f53\uff0cHTML\u88ab\u8f6c\u4e49\u6210\u4e86\u5b89\u5168\u7684\u666e\u901a\u6587\u672c\u663e\u793a\u4e86\uff0c\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201709\/2017-09-27_223823.png\" width=\"323\" height=\"200\" alt=\"HTML\u5b57\u7b26\u4e32\u88ab\u6587\u672c\u5316\u5904\u7406\" class=\"alignnone\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u51fa\u539f\u751fDOM\u7684<code>before()<\/code> API\u548cjQuery\u4e2d\u7684<code>before()<\/code> API\u8fd8\u662f\u6709\u5dee\u522b\u7684\uff0c\u5728jQuery\u4e2d\uff0c<code>before()<\/code>\u7684\u53c2\u6570\u503c\u662f\u4f5c\u4e3ahtml\u5b57\u7b26\u5904\u7406\u7684\uff0c\u4f46\u662f\u8fd9\u91cc\u7684<code>before()<\/code>\u662f\u4f5c\u4e3atext\u5b57\u7b26\u5904\u7406\u7684\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u60f3\u8981\u5728\u56fe\u7247\u524d\u9762\u63d2\u5165HTML\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528DOM\u8282\u70b9\u65b9\u5f0f\u63d2\u5165\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>var eleTitle = document.createElement('h4');\neleTitle.innerHTML = '\u7f8e\u5973\uff1a';\ndocument.getElementById('img').before(eleTitle);<\/pre>\n<p>\u5219\u4f1a\u8868\u73b0\u5982\u4e0b\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201709\/2017-09-27_230503.png\" width=\"314\" height=\"215\" alt=\"brfore()\u63d2\u5165h4\u6807\u9898\u540e\u6548\u679c\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u53ef\u80fd\u6709\u4eba\u8981\u7591\u95ee\u4e86\uff0c\u6211\u975e\u8981\u5728\u56fe\u7247\u524d\u9762\u63d2\u5165HTML\u5b57\u7b26\u5185\u5bb9\u600e\u4e48\u529e\uff1f<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u517c\u5bb9\u6027\u66f4\u597d\u7684<code>insertAdjacentHTML()<\/code> DOM\u65b9\u6cd5\uff0c\u4f7f\u7528\u793a\u610f\u5982\u4e0b\uff1a<\/p>\n<pre>document.getElementById('img').insertAdjacentHTML('beforebegin', '&lt;strong&gt;\u7f8e\u5973\uff1a&lt;\/strong&gt;');<\/pre>\n<p>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>element.insertAdjacentHTML(position, html);<\/pre>\n<p>\u975e\u672c\u6587\u91cd\u70b9\u5c31\u4e0d\u5c55\u5f00\u4e86\u3002<\/p>\n<p>\u5143\u7d20DOM\u7684<code>before()<\/code> API\u8fd8\u6709\u4e00\u4e2a\u5f88\u68d2\u7684\u7279\u6027\uff0c\u90a3\u5c31\u662f\u53ef\u4ee5\u540c\u65f6\u63d2\u5165\u591a\u4e2a\u8282\u70b9\u5185\u5bb9\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>document.getElementById('img').before('1. \u7f8e\u5973', ' ', '2. \u7f8e\u5973');<\/pre>\n<p>\u6548\u679c\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201709\/2017-09-27_231158.png\" width=\"253\" height=\"170\" alt=\"\u540c\u65f6\u63d2\u5165\u591a\u4e2a\u8282\u70b9\u5185\u5bb9\u622a\u56fe\u793a\u610f\" class=\"alignnone\" \/><\/p>\n<p><strong>demo\u6f14\u793a<\/strong><br \/>\n\u4ee5\u4e0a\u6240\u6709\u51fa\u73b0\u7684<code>before()<\/code>\u6548\u679c\u90fd\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\u4f53\u9a8c\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201709\/dom-api-before().html\" rel=\"noopener\" target=\"_blank\">DOM before()\u8282\u70b9API\u65b9\u6cd5demo<\/a><\/p>\n<p><strong>\u517c\u5bb9\u6027<\/strong><br \/>\n<code>before()<\/code> API Chrome54+\uff0cFirefox49+\u624d\u652f\u6301\uff0c\u8fd8\u7b97\u6bd4\u8f83\u65b0\uff0cIE edge\u76ee\u524d\u8fd8\u672a\u652f\u6301\uff0c\u5177\u4f53\u53c2\u89c1\u4e0b\u9762\u622a\u56fe\uff08<a href=\"http:\/\/caniuse.com\/#feat=dom-manip-convenience\" rel=\"noopener\" target=\"_blank\">\u5b9e\u65f6\u517c\u5bb9\u6570\u636e<\/a>\uff09\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201709\/2017-09-27_232127.png\" width=\"500\" height=\"367\" alt=\"before()\u7b49\u5c5e\u6027\u517c\u5bb9\u6027\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u5bf9\u4e8e\u56e2\u961f\u6216\u516c\u53f8\u5185\u90e8\u7684\u4e00\u4e9b\u9879\u76ee\uff0c\u7ba1\u7406\u5e73\u53f0\u6216\u8005\u5de5\u5177\u4e4b\u7c7b\u7684web\u9875\u9762\u6211\u4eec\u53ef\u4ee5\u653e\u5fc3\u5927\u80c6\u4f7f\u7528<code>before()<\/code>\u7b49API\uff0c\u5982\u679c\u662f\u9762\u5411\u7528\u6237\u5bf9\u517c\u5bb9\u6027\u6709\u8981\u6c42\u7684\u9879\u76ee\u5462\uff1f<\/p>\n<p>\u5f88\u7b80\u5355\uff0c\u52a0\u4e00\u6bb5polyfill JS\u4ee3\u7801\u5c31\u53ef\u4ee5\u4e86\uff0c\u5982\u4e0b\uff08\u53c2\u8003\u81ea<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/ChildNode\/before\" rel=\"noopener\" target=\"_blank\">MDN<\/a>\uff09\uff1a<\/p>\n<pre><span style=\"color:green;\">\/\/ \u6e90\u81ea https:\/\/github.com\/jserz\/js_piece\/blob\/master\/DOM\/ChildNode\/before()\/before().md<\/span>\n(function (arr) {\n  arr.forEach(function (item) {\n    if (item.hasOwnProperty('before')) {\n      return;\n    }\n    Object.defineProperty(item, 'before', {\n      configurable: true,\n      enumerable: true,\n      writable: true,\n      value: function before() {\n        var argArr = Array.prototype.slice.call(arguments),\n          docFrag = document.createDocumentFragment();\n        \n        argArr.forEach(function (argItem) {\n          var isNode = argItem instanceof Node;\n          docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));\n        });\n        \n        this.parentNode.insertBefore(docFrag, this);\n      }\n    });\n  });\n})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);<\/pre>\n<p>\u6ce8\u610f\uff0c\u4e0a\u9762\u7684polyfill \u5e76\u4e0d\u652f\u6301IE8\u53ca\u5176\u4ee5\u4e0b\u6d4f\u89c8\u5668\u3002\u4e5f\u5c31\u662f<code>before()<\/code> API\u53ea\u80fd\u5728\u81f3\u5c11\u517c\u5bb9\u5230IE9\u6d4f\u89c8\u5668\u7684\u9879\u76ee\u4f7f\u7528\u3002<\/p>\n<p><strong>\u548cinsertBefore()\u6bd4\u8f83<\/strong><br \/>\n<code>insertBefore()<\/code>\u4f5c\u4e3a\u8001\u724c\u4f20\u7edf\u7684API\uff0c\u4f18\u52bf\u5728\u4e8e\u517c\u5bb9\u6027\u597d\u3002\u4e0d\u8db3\u4e4b\u5904\uff0c\u5176\u8bed\u6cd5\u7740\u5b9e\u5f88\u5947\u602a\uff0cA\u5143\u7d20\u63d2\u5230B\u5143\u7d20\u524d\u9762\uff0c\u9700\u8981\u7236\u5143\u7d20<code>parentNode.insertBefore(newNode, referenceNode)<\/code>\uff0c\u5c0f\u8f88\u4e4b\u95f4\u7684\u6253\u6253\u95f9\u95f9\u7275\u626f\u5230\u7236\u8f88\uff0c\u663e\u7136\u4e8b\u60c5\u5c31\u4f1a\u9ebb\u70e6\u3002\u81f3\u5c11\u8fd9\u4e48\u591a\u5e74\u4e0b\u6765<code>insertBefore<\/code>\u7684\u53c2\u6570\u7a76\u7adf\u662f\u65b0\u63d2\u5165\u8282\u70b9\u5728\u524d\u8fd8\u662f\u5148\u63d2\u5165\u8282\u70b9\u5728\u540e\uff0c\u6211\u90fd\u6ca1\u6709\u51c6\u786e\u8bb0\u6e05\u695a\u3002<\/p>\n<p>\u4f46\u662f\uff0c<code>before()<\/code> API\u5c31\u4e0d\u4e00\u6837\u4e86\uff0c\u8bed\u6cd5\u4ec5\u6d89\u53ca\u5230\u63d2\u5165\u8282\u70b9\u548c\u76f8\u5bf9\u8282\u70b9\uff0c\u975e\u5e38\u597d\u8bb0\uff0c\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u800c\u4e14API\u540d\u79f0\u66f4\u77ed\u3002<\/p>\n<h4>2. DOM API\u4e4bafter()<\/h4>\n<p><code>after()<\/code>\u548c<code>before()<\/code>\u7684\u8bed\u6cd5\u7279\u6027\u517c\u5bb9\u6027\u90fd\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\uff0c\u5dee\u522b\u5c31\u5728\u4e8e\u8bed\u4e49\u4e0a\uff0c\u4e00\u4e2a\u662f\u5728\u524d\u9762\u63d2\u5165\uff0c\u4e00\u4e2a\u662f\u5728\u540e\u9762\u63d2\u5165\u3002<\/p>\n<p>\u7531\u4e8e\u8bed\u6cd5\u7c7b\u4f3c\uff0c\u56e0\u6b64\uff0c\u5c31\u4e0d\u4e00\u4e2a\u4e00\u4e2a\u793a\u610f\u4e86\uff0c\u82e5\u60f3\u76f4\u89c2\u4f53\u9a8c<code>after()<\/code>\u7684\u7279\u6027\u8868\u73b0\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u7684\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201709\/dom-api-after().html\" rel=\"noopener\" target=\"_blank\">DOM after()\u8282\u70b9API\u65b9\u6cd5demo<\/a><\/p>\n<p>\u7136\u540e\u4e0b\u9762\u7684JS\u4ee3\u7801\u662f\u5bf9<code>after()<\/code> API\u7684polyfill\uff0c\u540c\u6837\u5730\uff0c\u81f3\u5c11IE9+\u6d4f\u89c8\u5668\u3002<\/p>\n<pre><span style=\"color:green;\">\/\/\u6e90\u81ea https:\/\/github.com\/jserz\/js_piece\/blob\/master\/DOM\/ChildNode\/after()\/after().md<\/span>\n(function (arr) {\n  arr.forEach(function (item) {\n    if (item.hasOwnProperty('after')) {\n      return;\n    }\n    Object.defineProperty(item, 'after', {\n      configurable: true,\n      enumerable: true,\n      writable: true,\n      value: function after() {\n        var argArr = Array.prototype.slice.call(arguments),\n          docFrag = document.createDocumentFragment();\n        \n        argArr.forEach(function (argItem) {\n          var isNode = argItem instanceof Node;\n          docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));\n        });\n        \n        this.parentNode.insertBefore(docFrag, this.nextSibling);\n      }\n    });\n  });\n})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);<\/pre>\n<h4>3. DOM API\u4e4breplaceWith()<\/h4>\n<p>\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>ChildNode.replaceWith((\u8282\u70b9\u6216\u5b57\u7b26\u4e32)... \u66f4\u591a\u8282\u70b9);<\/pre>\n<p>\u8868\u793a\u66ff\u6362\u5f53\u524d\u8282\u70b9\u4e3a\u5176\u4ed6\u8282\u70b9\u5185\u5bb9\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u628a\u9875\u9762\u4e0a\u6240\u6709HTML\u6ce8\u91ca\u90fd\u66ff\u6362\u6210\u53ef\u663e\u793a\u7684\u666e\u901a\u6587\u672c\u8282\u70b9\u3002<\/p>\n<p>\u5982\u4e0bJS\u4ee3\u7801\uff1a<\/p>\n<pre>var treeWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_COMMENT);    \n\nwhile (treeWalker.nextNode()) {\n    var commentNode = treeWalker.currentNode;\n    <span style=\"color:green;\">\/\/ \u66ff\u6362\u6ce8\u91ca\u8282\u70b9\u4e3a\u6587\u672c\u8282\u70b9<\/span>\n    commentNode.replaceWith(commentNode.data);\n}<\/pre>\n<p>\u4f8b\u5982\u9875\u9762\u4e0a\u6709\u8fd9\u4e48\u4e00\u6bb5\u6ce8\u91ca\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201709\/2017-09-28_001117.png\" width=\"407\" height=\"125\" alt=\"\u9875\u9762\u4e0a\u7684\u6ce8\u91ca\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u70b9\u51fb\u67d0\u6309\u94ae\u6267\u884c\u4e0a\u9762\u7684JS\u4ee3\u7801\uff0c\u7ed3\u679c\u8fd9\u6bb5\u6ce8\u91ca\u5185\u5bb9\u53d8\u6210\u666e\u901a\u6587\u672c\u663e\u793a\u5728\u9875\u9762\u4e0a\u4e86\uff0c\u6548\u679c\u5982\u4e0b\u622a\u56fe\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201709\/2017-09-28_001228.png\" width=\"318\" height=\"278\" alt=\"\u6ce8\u91ca\u53d8\u6210\u666e\u901a\u6587\u672c\u663e\u793a\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u7684\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201709\/dom-api-replacewith().html\" rel=\"noopener\" target=\"_blank\">DOM replaceWith()\u8282\u70b9API\u65b9\u6cd5demo<\/a><\/p>\n<p>\u5bf9\u6211\u4eec\u5f00\u53d1\u6709\u6ca1\u6709\u4ec0\u4e48\u542f\u793a\u5462\uff1f\u6bd4\u65b9\u8bf4\u9875\u9762\u6a21\u677f\u53ef\u4ee5\u653e\u5728\u6ce8\u91ca\u4e2d\u2026\u2026<\/p>\n<p>\u540c\u6837\uff0c\u5982\u679c\u5bf9\u517c\u5bb9\u6027\u53c8\u8981\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684JS polyfill\uff08\u53c2\u8003\u81ea<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/ChildNode\/replaceWith\" rel=\"noopener\" target=\"_blank\">MDN<\/a>\uff09\uff1a<\/p>\n<pre>function ReplaceWith(Ele) {\n  var parent = this.parentNode,\n      i = arguments.length,\n      firstIsNode = +(parent && typeof Ele === 'object');\n  if (!parent) return;\n  \n  while (i-- > firstIsNode){\n    if (parent && typeof arguments[i] !== 'object'){\n      arguments[i] = document.createTextNode(arguments[i]);\n    } if (!parent && arguments[i].parentNode){\n      arguments[i].parentNode.removeChild(arguments[i]);\n      continue;\n    }\n    parent.insertBefore(this.previousSibling, arguments[i]);\n  }\n  if (firstIsNode) parent.replaceChild(this, Ele);\n}\nif (!Element.prototype.replaceWith) {\n\tElement.prototype.replaceWith = ReplaceWith;\n}\n    \nif (!CharacterData.prototype.replaceWith) {\n    CharacterData.prototype.replaceWith = ReplaceWith;\n}\nif (!DocumentType.prototype.replaceWith) {\n    CharacterData.prototype.replaceWith = ReplaceWith;\n}<\/pre>\n<h4>4. DOM API\u4e4bprepend()<\/h4>\n<p>\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>ParentNode.prepend((\u8282\u70b9\u6216\u5b57\u7b26\u4e32)... \u66f4\u591a\u8282\u70b9);<\/pre>\n<p>\u8868\u793a\u5728\u5f53\u524d\u8282\u70b9\u7684\u6700\u524d\u9762\u63d2\u5165\u5176\u5b83\u8282\u70b9\u5185\u5bb9\uff08\u4f5c\u4e3a\u5b50\u8282\u70b9\uff09\u3002\u5176\u610f\u601d\u548cjQuery\u4e2d\u7684<code>prepend()<\/code> API\u662f\u4e00\u6837\u7684\uff0c\u5bf9jQuery\u719f\u6089\u7684\u4eba\u5b66\u4e60\u8fd9\u51e0\u4e2aAPI\u90fd\u662f\u5206\u5206\u949f\u7684\u4e8b\u60c5\u3002<\/p>\n<p>\u53c2\u6570\u503c\u7279\u6027\u4ec0\u4e48\u7684\u548c<code>before()<\/code>, <code>after()<\/code>\u7b49\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u5c31\u4e0d\u91cd\u590d\u5c55\u5f00\u3002<\/p>\n<p>\u82e5\u6709\u5174\u8da3\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201709\/dom-api-prepend().html\" rel=\"noopener\" target=\"_blank\">DOM prepend()\u8282\u70b9API\u65b9\u6cd5demo<\/a><\/p>\n<p>\u5bf9demo\u56e0\u4e3a\u4e0a\u7684\u6309\u94ae\u4e71\u70b9\u4e00\u901a\u4e4b\u540e\uff0c\u4f1a\u53d1\u73b0\u6240\u6709\u63d2\u5165\u5185\u5bb9\u90fd\u5728\u6700\u524d\u9762\u663e\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201709\/2017-09-28_003613.png\" width=\"287\" height=\"250\" alt=\"prepend API demo\u9875\u9762\u70b9\u51fb\u622a\u56fe\u793a\u610f\" class=\"alignnone\" \/><\/p>\n<p>\u5728\u4ee5\u524d\u8981\u60f3\u5728\u5143\u7d20\u8282\u70b9\u7684\u6700\u524d\u9762\u63d2\u5165\u5185\u5bb9\uff0c\u8981\u4e48\u4f7f\u7528<code>insertBefore()<\/code>\u627e<code>firstChild<\/code>\uff0c\u8981\u4e48\u4f7f\u7528<code>insertAdjacentHTML()<\/code>\u6216\u8005<code>insertAdjacentElement()<\/code>\u65b9\u6cd5\uff0c\u90fd\u5f88\u5570\u55e6\u3002<\/p>\n<p><strong>prepend()<\/strong>\u8fd9\u4e2aapi\u8981\u66f4\u7b80\u5355\u548c\u76f4\u63a5\u3002<\/p>\n<p>\u517c\u5bb9\u6027\u548c<code>before()<\/code>\u4e00\u6a21\u4e00\u6837\uff0c\u5bf9\u4e8eIE9+\u652f\u6301\u9879\u76ee\uff0c\u53ef\u4ee5\u8f85\u52a9\u4e0b\u9762\u7684polyfill\uff1a<\/p>\n<pre><span style=\"color:green;\">\/\/ \u6e90\u81ea\uff1a https:\/\/github.com\/jserz\/js_piece\/blob\/master\/DOM\/ParentNode\/prepend()\/prepend().md<\/span>\n(function (arr) {\n  arr.forEach(function (item) {\n    if (item.hasOwnProperty('prepend')) {\n      return;\n    }\n    Object.defineProperty(item, 'prepend', {\n      configurable: true,\n      enumerable: true,\n      writable: true,\n      value: function prepend() {\n        var argArr = Array.prototype.slice.call(arguments),\n          docFrag = document.createDocumentFragment();\n        \n        argArr.forEach(function (argItem) {\n          var isNode = argItem instanceof Node;\n          docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));\n        });\n        \n        this.insertBefore(docFrag, this.firstChild);\n      }\n    });\n  });\n})([Element.prototype, Document.prototype, DocumentFragment.prototype]);<\/pre>\n<h4>5. DOM API\u4e4bappend()<\/h4>\n<p>\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>ParentNode.append((\u8282\u70b9\u6216\u5b57\u7b26\u4e32)... \u66f4\u591a\u8282\u70b9);<\/pre>\n<p>\u8868\u793a\u5728\u5f53\u524d\u8282\u70b9\u7684\u6700\u540e\u9762\u63d2\u5165\u5176\u5b83\u8282\u70b9\u5185\u5bb9\uff08\u4f5c\u4e3a\u5b50\u8282\u70b9\uff09\u3002\u5176\u610f\u601d\u548cjQuery\u4e2d\u7684<code>append()<\/code> API\u662f\u4e00\u6837\u7684\uff0c\u7ec6\u8282\u4e0a\u5c31\u662f\u4e0d\u652f\u6301html\u5b57\u7b26\u4e32\u76f4\u63a5\u663e\u793a\u7684\u5dee\u522b\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201709\/dom-api-append().html\" rel=\"noopener\" target=\"_blank\">DOM append()\u8282\u70b9API\u65b9\u6cd5demo<\/a><\/p>\n<p>\u6240\u6709\u70b9\u51fb\u6309\u94ae\u63d2\u5165\u5185\u5bb9\u90fd\u5728\u56fe\u7247\u540e\u9762\uff0c\u4e5f\u5c31\u662f\u5bb9\u5668\u91cc\u9762\u7684\u6700\u540e\u663e\u793a\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201709\/2017-09-28_004902.png\" width=\"334\" height=\"143\" alt=\"append()  demo\u9875\u9762\u6548\u679c\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>polyfill\u5982\u4e0b\uff1a<\/p>\n<pre><span style=\"color:green;\">\/\/ \u6e90\u81ea\uff1a https:\/\/github.com\/jserz\/js_piece\/blob\/master\/DOM\/ParentNode\/append()\/append().md<\/span>\n(function (arr) {\n  arr.forEach(function (item) {\n    if (item.hasOwnProperty('append')) {\n      return;\n    }\n    Object.defineProperty(item, 'append', {\n      configurable: true,\n      enumerable: true,\n      writable: true,\n      value: function append() {\n        var argArr = Array.prototype.slice.call(arguments),\n          docFrag = document.createDocumentFragment();\n        \n        argArr.forEach(function (argItem) {\n          var isNode = argItem instanceof Node;\n          docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));\n        });\n        \n        this.appendChild(docFrag);\n      }\n    });\n  });\n})([Element.prototype, Document.prototype, DocumentFragment.prototype]);<\/pre>\n<h3>\u4e09\u3001\u7ed3\u675f\u8bed<\/h3>\n<p>\u6211\u4eec\u4e3a\u4ec0\u4e48\u4f7f\u7528jQuery\uff0c\u4e00\u662fDOM API\u7684\u4fbf\u5229\u6027\uff0c\u4e8c\u662f\u5185\u90e8\u5e2e\u6211\u4eec\u641e\u5b9a\u4e86\u5f88\u591a\u517c\u5bb9\u6027\u95ee\u9898\uff0c\u4e09\u662f\u53cb\u597d\u5730\u6269\u5c55\u548c\u63d2\u4ef6\u673a\u5236\u3002<\/p>\n<p>\u5982\u4eca\uff0c\u539f\u751f\u7684DOM API\u501f\u9274jQuery\u7684\u4f18\u70b9\uff0c\u4e5f\u6574\u51fa\u4e86\u5f88\u591a\u7b80\u5355\u904d\u5386\u7684API\u65b9\u6cd5\uff1b\u5982\u679c\u6211\u4eec\u518d\u7c98\u8d34\u4e00\u4e9bpolyfill JS\u641e\u5b9a\u517c\u5bb9\u6027\u95ee\u9898\uff1b\u518d\u914d\u5408ES5\u5f88\u591a\u6570\u636e\u5904\u7406\u65b9\u6cd5\uff1bDOM leave 3\u7684\u4e8b\u4ef6\u5904\u7406\u7b49\u3002<\/p>\n<p>\u57fa\u672c\u4e0a\u5c31\u6ca1\u6709\u9700\u8981\u4f7f\u7528jQuery\u7684\u7406\u7531\u4e86\uff0c\u7701\u4e86\u8d44\u6e90\u52a0\u8f7d\uff0c\u63d0\u9ad8\u4e86\u4ee3\u7801\u6027\u80fd\uff0c\u8fd8\u88c5\u4e86\u56de\u54d4\u54e9\u54d4\u54e9\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u57fa\u672c\u4e0a\uff0c\u5df2\u7ecf\u4e0d\u53ef\u9006\u5730\uff0c\u5728\u4e0d\u4e45\u5c06\u6765\uff0c\u4e0d\u51fa\u51e0\u5e74\uff0c\u884c\u4e1a\u4f1a\u5174\u8d77\u539f\u751fDOM API\uff0c\u539f\u751fJS\u5f00\u53d1\u524d\u7aef\u5e94\u7528\u7684\u5c0f\u98ce\u5c1a\uff0cjQuery\u4f1a\u8d8a\u6765\u8d8a\u4e0d\u88ab\u4eba\u63d0\u8d77\uff0c\u5b8c\u6210\u5176\u5386\u53f2\u4f7f\u547d\uff0c\u65e5\u540e\u53ef\u4ee5\u9886\u53d6\u4e2a\u7ec8\u8eab\u6210\u5c31\u5956\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u8fd8\u6709\u7c7bjQuery\u7684API\u672c\u6587\u6ca1\u6709\u4ecb\u7ecd\uff0c\u4f8b\u5982<code>Node.remove()<\/code>\uff0c\u8fd9\u4e2aAPI\u51fa\u73b0\u76f8\u5bf9\u8981\u65e9\u4e00\u4e9b\uff0c\u548c\u524d\u9762\u4ecb\u7ecd\u7684\u4e0d\u662f\u4e00\u6ce2\u51fa\u6765\u7684\uff0c\u56e0\u6b64\u6ca1\u653e\u5230\u4e00\u8d77\u4ecb\u7ecd\u3002<\/p>\n<p>\u4f5c\u7528\u5982\u5176\u540d\uff0c\u79fb\u9664\u5f53\u524d\u8282\u70b9\u3002<\/p>\n<p>\u597d\uff0c\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u90fd\u662f\u6d45\u8584\u7684\u77e5\u8bc6\u70b9\u3002<\/p>\n<p>\u611f\u8c22\u9605\u8bfb\uff0c\u6b22\u8fce\u4ea4\u6d41\uff01<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201611\/14.png\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=6443\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=6443<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a5\u4e0b\u6765\u8981\u4ecb\u7ecd\u8fd9\u4e9b\u65b0\u589e\u7684DOM API\u65b9\u6cd5\uff0c\u90fd\u6bd4\u8f83\u65b0\uff0c\u5176\u8bbe\u8ba1\u76ee\u7684\u662f\u53ef\u4ee5\u50cfjQuery\u90a3\u6837\uff0c\u4f7f\u7528\u975e\u5e38\u7b80\u5355\u7684api\uff0c\u4fbf\u5229\u7684\u64cd\u4f5cdom\u5143\u7d20\u3002<\/p>\n<p>\u8fd9\u4e9bapi\u5305\u62ec\uff1a<code>before()<\/code>\uff0c<code>after()<\/code>\uff0c<code>replaceWith()<\/code>\uff0c<code>append()<\/code>\uff0c<code>prepend()<\/code>\u2026\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[319,366,215,77,1172,182,79,14,1171,1170],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/6443"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=6443"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/6443\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=6443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=6443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=6443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}