复制代码 代码如下: <!-- beforebegin --><p id="test"><!-- afterbegin -->hello, I am a demo.<!-- beforeend--></p><!-- afterend--> $("test").insertAdjacentHTML("beforebegin", /* your content here */);
复制代码 代码如下: function textContent(e) { var child, type, s = []; // s holds the text of all children for(child = e.firstChild; child != null; child = child.nextSibling) { type = child.nodeType; if(type === 3 || type === 4) { //Text and CDATASection nodes s.push(child.nodeValue); } else if(type === 1) { s.push(textContent(child)); } return s.join(""); } }