我在网页中使用jQuery.在Internet Explorer中使用$时,它可以正常工作.在Chrome或Firefox中引用$时,它会失败并显示错误:
Uncaught ReferenceError: $is not defined.
截图:
用我的源代码:
<!DOCTYPE html> <html> <head> <script type="text/javascript" src="jquery-1.7.2.min.js"></script> <script type="text/javascript"> function divClick(sender,event) { // var chk = $(sender).find("input").first()[0]; var chk = jQuery(sender).find("input").first()[0]; alert("Works in ie"); } </script> </head> <body> <div onclick="divClick(this,event)"> <input type="checkBox"> </div> </body> </html>
更新:尝试将其更改为jQuery.
解决方法
这个问题只是为了记录Chrome和Firefox中的错误:
Html File encoding IE9 Chrome ========================= ======= ====== Windows-1252 Works Works UTF-8 (without BOM) Works Works UTF-8 (with BOM EFBB) Works Works UTF-16 (with LE BOM FFFE) Works Fails UTF-16 (with BE BOM FEFF) Works Fails
据推测,Chrome(和Firefox)假设单独的脚本文件与html文件具有相同的编码.
然后Chrome尝试将jquery-1.7.2.js作为UTF-16读取,并且发现该文件是纯粹的(Windows-1252)垃圾是令人震惊的.