1.替换两个node:
function replaceEach(aNode, bNode) { var aParent = aNode.parentNode; var bParent = bNode.parentNode; if (aParent && bParent){ var aNode2 = aNode.cloneNode(true); bParent.replaceChild(aNode2, bNode); aParent.replaceChild(bNode, aNode); } }
2.自定义insertAfter方法:
function insertAfter(newNode, refNode) { var parentNode = refNode.parentNode; if (parentNode){ if (refNode == parentNode.lastChild) { parentNode.appendChild(newNode); } else { parentNode.insertBefore(newNode, refNode.nextSibling); } } }
3.除去文本两边的空格:
var reg = /^\s*|\s*$/g; textContent = textContent.replace(reg, "");