admin管理员组

文章数量:1405603

Our forms software generates multiple <style> tags in the <head> section, none of which are needed (or wanted).

How can I remove them? I tried the following but guess my logic is flawed here...I thought I had to target the parent (the <head>) to remove a child element but guess I'm being too simplistic:

var hs = document.getElementsByTagName('style');
for (var i=0, max=all.length; i < max; i++) {
    hs[i].parentNode.removeChild(hs[i]);
}

Have I got myself in an array muddle?

Our forms software generates multiple <style> tags in the <head> section, none of which are needed (or wanted).

How can I remove them? I tried the following but guess my logic is flawed here...I thought I had to target the parent (the <head>) to remove a child element but guess I'm being too simplistic:

var hs = document.getElementsByTagName('style');
for (var i=0, max=all.length; i < max; i++) {
    hs[i].parentNode.removeChild(hs[i]);
}

Have I got myself in an array muddle?

Share Improve this question edited Feb 21, 2014 at 13:06 putvande 15.2k3 gold badges36 silver badges51 bronze badges asked Feb 21, 2014 at 13:05 neilneil 1,3062 gold badges12 silver badges21 bronze badges 2
  • hs[i].remove()? But your code works fine for me. – putvande Commented Feb 21, 2014 at 13:07
  • 1 I think you have to access the parent to remove its child, rather than remove the child directly? – neil Commented Feb 21, 2014 at 14:12
Add a ment  | 

1 Answer 1

Reset to default 4

Try

var hs = document.getElementsByTagName('style');
for (var i=0, max = hs.length; i < max; i++) {
    hs[i].parentNode.removeChild(hs[i]);
}

You used max = all.length, you did not define all and I'm guessing you meant hs.length. And max = hs.length would be one to many because the array is 0 based.

本文标签: javascriptRemoving ltstylegt tags from ltheadgtStack Overflow