html - <div> tag to <img> tag Javascript -
hello possible change div tags image tags?
i tried using this
var testdivtags= document.getelementsbyclassname('div_tags'); var testimgtag= document.createelement("img"); (var = 0; < testdivtags.length; ++i) { testdivtags[i].parentnode.replacechild(testimgtag, testdivtags[i]) }
but doesn't work. ideas?
the problem element can't @ different places in dom simultaneously.
instead, should clone element, , insert clones:
parent.replacechild(newchild.clonenode(), oldchild)
moreover, there problem: htmlcollection
returned getelementsbyclassname
live. therefore, when replace elements, disappear list, , following ones reindexed lower indices. fix that, can
iterate live
htmlcollection
collection backwards:var livec = document.getelementsbyclassname('div_tags'); (var = livec.length-1; >= 0; --i) livec[i].parentnode.replacechild(testimgtag.clonenode(), livec[i]);
convert array:
var array = [].slice.call(document.getelementsbyclassname('div_tags')); (var = 0; < array.length; ++i) array[i].parentnode.replacechild(testimgtag.clonenode(), array[i]);
use
queryselectorall
, returns staticnodelist
collection:var staticc = document.queryselectorall('.div_tags'); (var = 0; < staticc.length; ++i) staticc[i].parentnode.replacechild(testimgtag.clonenode(), staticc[i]);
Comments
Post a Comment