JS: Failed to execute 'getComputedStyle' on 'Window': parameter is not of type 'Element'

前端 未结 6 1808
北海茫月
北海茫月 2020-12-09 14:56

In short: I am trying to understand the meaning of this TypeError: Failed to execute \'getComputedStyle\' on \'Window\': parameter 1 is not of type \'Element\' The error app

6条回答
  •  孤城傲影
    2020-12-09 15:39

    In my case I was using ClassName.

    getComputedStyle( document.getElementsByClassName(this_id)) //error
    

    It will also work without 2nd argument " ".

    Here is my complete running code :

    function changeFontSize(target) {
    
      var minmax = document.getElementById("minmax");
    
      var computedStyle = window.getComputedStyle
            ? getComputedStyle(minmax) // Standards
            : minmax.currentStyle;     // Old IE
    
      var fontSize;
    
      if (computedStyle) { // This will be true on nearly all browsers
          fontSize = parseFloat(computedStyle && computedStyle.fontSize);
    
          if (target == "sizePlus") {
            if(fontSize<20){
            fontSize += 5;
            }
    
          } else if (target == "sizeMinus") {
            if(fontSize>15){
            fontSize -= 5;
            }
          }
          minmax.style.fontSize = fontSize + "px";
      }
    }
    
    
    onclick= "changeFontSize(this.id)"
    

提交回复
热议问题