【发布时间】:2017-08-03 00:09:15
【问题描述】:
在高级模式下通过closure-compiler 优化我的 JS 时收到警告消息并且未编译。
JSC_TYPE_MISMATCH:实际参数 1 Document.prototype.getElementById 与形参不匹配
我的 js 函数来改变 div 的类
for (kx = 1; kx <= 5;kx=kx+1) {
document.getElementById(kx).className='newclass';
}
在 HTML 中我有五个 div 如下
<div id='1' class ='first'> contents </div>
<div id='2' class ='first'> contents </div>
<div id='3' class ='first'> contents </div>
<div id='4' class ='first'> contents </div>
<div id='5' class ='first'> contents </div>
此代码在正常情况下工作(没有压缩/优化),但在尝试优化它时显示警告/错误,我该如何修复它?
【问题讨论】:
-
我认为这是 Closure 中的一个错误,因为您所做的将在实际浏览器中完美运行。 HTML5 之前的规范认为“id”属性应该是有效的标识符,但在 HTML5(以及所有现代浏览器)中并非如此 - 任何没有嵌入空格的字符串都是有效的。
-
@Pointy 我认为这与type of the argument有关。
-
@canon 是的,我同意,但我的意思是警告只是噪音,因为(如您所述)代码将起作用。
标签: javascript optimization google-closure-compiler