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
1 Answer
Reset to default 4Try
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
版权声明:本文标题:javascript - Removing <style> tags from <head> - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744905554a2631598.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论