日期:2014-05-16  浏览次数:20701 次

ext.get(id)取出来的值为null
刚学Extjs,按照教程写的代码,可是却出了个TypeError: Ext.get("btn") is null的错,下面是代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link rel="stylesheet" type="text/css" href="../../../ext-3.3.0/resources/css/ext-all.css"/>
<script type="text/javascript" src="../../../ext-3.3.0/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../../../ext-3.3.0/ext-all.js"></script>
<script type="text/javascript">
Ext.get("btn").dom.on(
"click",
function(){
Ext.MessageBox.show({
title:"时间进度条",
msg:"5s后关闭进度框",
process:true,
width:300,
height:400,
wait:true,
waitConfig:{
interval:600,
duration:5000,
fn:function(){
Ext.MessageBox.hide();
}
},
closable:true
});
}
);
</script>
</head>
<body>
<input type="button" id="btn" value="test" />
</body>
</html>

------最佳解决方案--------------------
引用:
可能是 那button还没有加载的时候 你就去get 了。lz可以试试在 把那段js代码放到 Ext.onReady()里去。
如:Ext.onReady(function(){
      lz上面js代码
});


对象还没生成,并且不需要获取dom对象,dom对象没有on方法,只有ext包装的对象才有on方法
Ext.onReady(function(){//////////
    Ext.get("btn").on(///////////
        "click",
        function(){
            Ext.MessageBox.show({
                title:"时间进度条",
                msg:"5s后关闭进度框",
                process:true,
                width:300,
                height:400,
                wait:true,
                waitConfig:{
                    interval:600,
                    duration:5000,
                    fn:function(){
                        Ext.MessageBox.hide();
                    }    
                },
  &