52,787
社区成员




<!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>
</head>
<style type="text/css">
</style>
<body>
<script type="text/javascript">
function f(){
alert( );
if(document.getElementsByName("pic").offsetHeight>200){
alert("<200")}
else{alert(">200")};
}
f();
</script>
<img src="pic3.jpg" name="pic">
</span>
</body>
</html>
哪位大侠给个例子?<!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=gb2312" />
<title>无标题文档</title>
</head>
<style type="text/css">
</style>
<body>
<script type="text/javascript">
function f(){
//document.getElementsByName返回的数组,不是dom对象,所以要取数组中的dom对象
//你的输出也错了吧,怎么>200输出<200......
if(document.getElementsByName("pic")[0].offsetHeight>200)
//alert("<200");
alert(">200");
else
//alert(">200");
alert("<200");
}
window.onload=f//===================
</script>
<img src="pic3.jpg" name="pic" style="height:100px">
</span>
</body>
</html>
自己测试下alert就知道了,你会更深刻写
因为放在onload中的是等待你页面所有的dom元素构建完毕后才触发事件,然后再执行代码的
如果你直接用f()会立即运行,页面会按照它的顺序执行代码,所以下面会找不到元素
或取不到高度