0
var $ = function (id){return document.getElementById(id);}
 window.onload=function(){
 $("num").onfocus=function(){
        $("show").style.display="block";
 }

未捕获的类型错误:未定义不是函数

但是当我将 $("num") 更改为 $("#num") 它可以工作,为什么?

4

3 回答 3

2

You must have jQuery loaded on your page, and you define $ as a local variable, so during the onload the $ variable take it's old signification which is jQuery. And jQuery uses the same selectors as CSS.

You can solve your problem by disabling jQuery if you don't need it, or setting it in compatibility mode, or setting your $ variable as global variable.

于 2013-08-21T08:31:20.213 回答
2

如果您的代码有效

a) 没有加载 jQuery

b)添加缺少的大括号

现场演示

var $ = function (id){return document.getElementById(id);} 

window.onload=function(){
   $("num").onfocus=function(){
        $("show").style.display="block";
   }       
 }
于 2013-08-21T09:25:28.537 回答
0

您很可能引用了无信息的 jQuery变量$名。原因是在引用框架(来源)window.jQuery的情况下,两者window.$都指向jQuery实例。 jQuery


事实上, next 应该按预期工作:

 window.onload=function(){
   var $ = function (id){return document.getElementById(id);} 
   $("num").onfocus=function(){
        $("show").style.display="block";
   }
 }
于 2013-08-21T08:36:32.223 回答