如何解决“未捕获的TypeError:无法将属性'onchange'设置为null”?

时间:2019-06-18 15:25:18

标签: javascript google-chrome-extension

尝试在我的计算机上本地上传扩展程序,并收到此错误“未捕获的TypeError:无法将属性'onchange'设置为null”。这很可能是因为窗口已加载,并且chrome扩展程序正在后台运行脚本(实际上不应该-仅当实际加载弹出窗口(单击时))

尝试查找其他人成功克服了这种情况的地方,尽管大多数人在其他领域或使用JQuery而不是Javascript解决方案。

代码如下:

    window.onload = function() {
    
    	var pVal = "";
      
      function func(item) { 
    		if (item == 1) {
    			pVal = "A";
    		} else if (item == 2) {
    			pVal = "B";
    		} else if (item == 3) {
    			pVal = "C";
    		} else if (item == 4) {
    			pVal = "D";
    		} else if (item == 5) {
    			pVal = "E";
    		} else if (item == 6) {
    			pVal = "F";
    		} else if (item == 7) {
    			pVal = "G";
    		} else if (item == 8) {
    			pVal = "H";
    		} else if (item == 9) {
    			pVal = "I";
    		} else if (item == 10) {
    			pVal = "J";
    		} 
    	}
      
    	document.getElementById("test1").onchange = function() { 
    		func(document.getElementById("test1").value);
    	}
      
      document.getElementById('buttonId').onclick = function() {
    		document.getElementById('TestId').innerHTML = pVal;
    	}
    
    }
        <!DOCTYPE html>
    <html>
    	<body>	
        <select id="test1" class="form-element" name="test1" tabindex="8" required>
          <option value="" selected disabled hidden>Select something</option>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
          <option value="5">5</option>
          <option value="6">6</option>
          <option value="7">7</option>
          <option value="8">8</option>
          <option value="9">9</option>
          <option value="10">10</option>
        </select>
    
        <br>
        <button id="buttonId" class="buttonClass">Does it</button>
    
    		<h3 id="TestId" class="sClass"></h3>
    
    	</body>
    	<script src="popup.js"></script>
    </html>

感谢您的全部帮助!

0 个答案:

没有答案