var canvas_object=$("#canvas_object");function show(){
alert(canvas_object.css("height"));
alert($("#canvas_object").css("height"));
}请教一下:第一个alert 显示 undefined
第二个alert 却能显示出来 正确的 height也就是说我从function外传进来的 变量不工作。 我如果直接用 jquery 读取这个 canvas_object就没问题。哪位能帮忙解释一下。 还有如果我必须在jquery中 把变量传递到 function中 我应该如何写?谢谢大家

解决方案 »

  1.   

    show("canvas_object");
    function show(name){
    alert($("#"+name).css("height"));
    }这样?
      

  2.   

    show的时候 给变量赋值  
    或者像楼上那样带个参数进来 ?
      

  3.   

    不知道你具体的情况,但是如果元素还未加载
    var canvas_object=$("#canvas_object")
    这样使用,是找不到的.
    所以后面就是undefined,而第二个alert 却能显示出来 正确的 height
      

  4.   

    var $canvas_object=$("#canvas_object");
    这样呢
      

  5.   

    to b327114069
    试了 也不行
    我把 testing 代码贴出来 大家帮忙看看<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            </style>
            <script type="text/javascript" src="jquery-1.4.2.0.js">
            </script>
    <style>
    #canvas_panel{height:200px; width:600px; border:1px #000 solid; margin:0 auto; }
    #canvas_object{background:#0000ff;width:50px; height:50px; position:absolute;} </style>
            <script type="text/javascript">
             var canvas_object=$("#canvas_object");
             function show(){
    alert(canvas_object.css("height"));
    alert($("#canvas_object").css("height"));
    }
             $(document).ready(function(){
    show();


    });
            </script>
        </head>
        <body>
         <script>

         </script>
            <div id="canvas_panel">
    <div id="canvas_object"></div>
    </div>
    <div>
    <button id="btn">Toggle between slide up and slide down for a p element</button>
    </div>
        </body>
    </html>
      

  6.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      </style>
      <script type="text/javascript" src="jquery-1.4.2.0.js">
      </script>
    <style>
    #canvas_panel{height:200px; width:600px; border:1px #000 solid; margin:0 auto; }
    #canvas_object{background:#0000ff;width:50px; height:50px; position:absolute;}</style>
      <script type="text/javascript">
      var canvas_object;
      function show(){
    alert(canvas_object.css("height"));
    alert($("#canvas_object").css("height"));
    }
      $(document).ready(function(){
     canvas_object=$("#canvas_object");
    show();
    });
      </script>
      </head>
      <body>
      <script>  </script>
      <div id="canvas_panel">
    <div id="canvas_object"></div>
    </div>
    <div>
    <button id="btn">Toggle between slide up and slide down for a p element</button>
    </div>
      </body>
    </html>
      

  7.   


    那是因为代码按照顺序执行,而页面的html代码还没有运行完毕。
    也就是说dom树还没有构建解释,你的那段代码就运行了。所以是undefined
    <html>
      <body>
              <element> more </element>
              <!-- 脚本最后执行,这样看看 -->
              <script>
                  var canvas_object=$("#canvas_object");              function show(){
                     alert(canvas_object.css("height"));
                     alert($("#canvas_object").css("height"));
                  }
              </script>
      </body>
    </html>在window.onload函数中运行
              <script>
                  var canvas_object;
                  window.onload = function () {
                       canvas_object = $("#canvas_object");
                  };              function show(){
                     alert(canvas_object.css("height"));
                     alert($("#canvas_object").css("height"));
                  }
              </script>
    在jquery的ready函数中运行
              <script>
                  var canvas_object;
                  $(function () {
                       canvas_object = $("#canvas_object");
                  });              function show(){
                     alert(canvas_object.css("height"));
                     alert($("#canvas_object").css("height"));
                  }
              </script>
      

  8.   

    谢谢 的确是 代码顺序的 问题 我把赋值卸载 jquery 的function ready 里就没问题了。好像脑子里还有很多写java 和c# 的痕迹。 写javascript是总是 忘记 要先创建 dom 的问题 
    谢谢 大家!!!!!!!!!!