How to force ie8 to repaint after adding a class to a dom element

后端 未结 6 896
旧时难觅i
旧时难觅i 2021-02-01 05:52

In ie8 if elements don\'t \'repaint\' with the associated css when you change the classname, how can you force the browser to refresh and not kill ie8 performance?

This

6条回答
  •  盖世英雄少女心
    2021-02-01 06:26

    I had a lot of difficulty and tried everything to no avail...until I tried this for IE8

    function toggleCheck(name) {
      el = document.getElementById(name);
      if( hasClass(el, 'checked') ) {
        el.className = el.className.replace(/checked/,'unchecked');
      } else if( hasClass(el, 'unchecked') ) {
        el.className = el.className.replace(/unchecked/,'checked');
      }
      document.body.className = document.body.className;
    }
    function hasClass(element, cls) {
      return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
    }
    

    And now my CSS changes for my checkboxes work beautifully for IE8, Chrome, and Firefox!

提交回复
热议问题