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
What 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:
content
createElement
and friends instead of innerHTML
This should also work:
var div = document.createElement('div');
div.innerHTML = content;
div.childElementCount && div.firstElementChild.style.display = '';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.