admin管理员组文章数量:1399950
I want to set up css display property in javascript code:
var div = document.createElement('div'); div.innerHTML = content; div.childNodes[0].style.display = '';
It works in IE but doesn't in FF. It says "style" is undefined for element div. How can I do it in FF?
Thanks
I want to set up css display property in javascript code:
var div = document.createElement('div'); div.innerHTML = content; div.childNodes[0].style.display = '';
It works in IE but doesn't in FF. It says "style" is undefined for element div. How can I do it in FF?
Thanks
Share Improve this question edited Dec 20, 2010 at 17:41 Pointy 414k62 gold badges595 silver badges629 bronze badges asked Dec 20, 2010 at 17:38 ihorkoihorko 6,95526 gold badges80 silver badges123 bronze badges 1-
1
What kind of node is
div.childNodes[0]
? – Pointy Commented Dec 20, 2010 at 17:40
2 Answers
Reset to default 5What is content
? If it starts with white space, then there will be a TextNode as the first child and they don't have style properties (HTMLElementNodes do).
You can either:
- loop over the children until you either get to the end or find an HTMLElementNode
- strip the whitespace from the start of
content
- switch to using
createElement
and friends instead ofinnerHTML
This should also work:
var div = document.createElement('div');
div.innerHTML = content;
div.childElementCount && div.firstElementChild.style.display = '';
本文标签: cssjavascript elementstyle is undefined in FFStack Overflow
版权声明:本文标题:css - javascript element.style is undefined in FF - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744198212a2594838.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论