简体   繁体   中英

How can I change style attribute using setAttribute instead of using style.cssText on IE8

I can change TR style on IE 8 with :

 _tr.style.cssText = "";

But I have a bunch of properties which are NOT ONLY STYLE (id, align, etc...) - I don't know at design time - to change and want to do all at once generically with setAttribute.

 _tr.setAttribute("style", "")

works in Chrome and IE Edge but not on IE 8. Is there another syntax for IE 8 ?

Update : I know IE8 is a nightmare but I'm on a big corp project who will only switch to modern IE at the end of the year.

You can use an object literal for your attributes and add them via for/in loop:

<div id="test">This is a test</div>
<script>
    var div = document.getElementById('test');
    var styles = {'color':'#ff0000', 'backgroundColor':'#00ff00', 'width':'500px', 'height':'250px'};
    for (var style in styles) {
        div.style[style] = styles[style];
    }
</script>

As a side question: Is a separate css class not the better solution?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM