javascript - Removing elements from a DOM copy -
i wanted remove element re-create of main dom cannot work.
basically trying remove h2 element duplicate dom , homecoming modified code in alert whatever have tried far original dom still.
<html> <head> </head> <body> <h2>test</h2> <script type="text/javascript" id="customscript"> var page = document.documentelement; var temppage = document.documentelement; temppage.removechild("h2"); // remove not working temppage.getelementbyid("h2").innerhtml = ""; // remove not working </script> <input type="button" value="get pages code" onclick="alert(temppage.innerhtml)"> </body> </html>
if possible rather not utilize jquery or yui etc , want utilize regular javascript.
there 2 parts question , doing them both incorrectly.
temppage
same object page
. assignment different variable not create copy--it lets access same object via name. need explicitly create copy. your attempts h2
element both incorrect. removechild()
won't work because: it requires node object, not string. documentelement
<html>
element, <h2>
element kid of <body>
, not <html>
. getelementbyid()
won't work because <h2>
element not have id attribute. how is:
var temppage = document.documentelement.clonenode(true); // re-create document var h2 = temppage.queryselector('h2'); // find h2 element h2.parentnode.removechild(h2); // remove h2 element
if queryselector
not available, much more tedious task still possible using normal dom manipulation. need learn dom manipulation
however finish code can't fathom why need clone page. this:
function textcontent(node) { homecoming node.textcontent || node.innertext || ''; } var h2 = document.getelementsbytagname('h2')[0]; var h2text = textcontent(h2);
javascript dom
No comments:
Post a Comment