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中 我应该如何写?谢谢大家
alert(canvas_object.css("height"));
alert($("#canvas_object").css("height"));
}请教一下:第一个alert 显示 undefined
第二个alert 却能显示出来 正确的 height也就是说我从function外传进来的 变量不工作。 我如果直接用 jquery 读取这个 canvas_object就没问题。哪位能帮忙解释一下。 还有如果我必须在jquery中 把变量传递到 function中 我应该如何写?谢谢大家
解决方案 »
- js实现可编辑表格问题。
- 菜鸟求助document.addEventListener和element.addEventListener有什么不同?
- * 请教: 如何把 input里面的字符串转成 UTF-8 ?
- javascript代码:object对象事件代码执行问题
- javascript object 对像的使用问题
- 如何用javascript将dropdownlist的当前项改为最后一项
- 只有最后20分了,求一具体表格删减js代码
- 好奇怪的问题?太怪异了!!!!!!!在线
- 请孟子E章大哥进来一下!
- 请问如何在asp语句中引用javascript
- 高分求解:如何通过代码设置jquery easyUI的menu菜单项为不可用?
- Table点击表头排序的js代码问题
function show(name){
alert($("#"+name).css("height"));
}这样?
或者像楼上那样带个参数进来 ?
var canvas_object=$("#canvas_object")
这样使用,是找不到的.
所以后面就是undefined,而第二个alert 却能显示出来 正确的 height
这样呢
试了 也不行
我把 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>
<!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>
那是因为代码按照顺序执行,而页面的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>
谢谢 大家!!!!!!!!!!