<html>
<head>
</head><body style="margin:0px;padding:0px;">
<script type="text/javascript">
<!--
var theElement=document.getElementById('capture');
function changeimg(address,width,height)
{
debugger;
theElement.setAttribute('src',address);
theElement.setAttribute('width',width);
theElement.setAttribute('height',height);
}
//-->
</script>
<img id="capture" name="capture" src="http://192.168.1.81:80/images/246.bmp" width="170" height="140" 、>
<input type="button" value="改变图像" onclick="changeimg("http://192.168.1.81:80/images/245.bmp",170,140)" />
</body></html>
这个运行到debugger;下面一行就写"theElement为空或者不是对象"
请问代码错到哪里了呢?谢谢
<head>
</head><body style="margin:0px;padding:0px;">
<script type="text/javascript">
<!--
var theElement=document.getElementById('capture');
function changeimg(address,width,height)
{
debugger;
theElement.setAttribute('src',address);
theElement.setAttribute('width',width);
theElement.setAttribute('height',height);
}
//-->
</script>
<img id="capture" name="capture" src="http://192.168.1.81:80/images/246.bmp" width="170" height="140" 、>
<input type="button" value="改变图像" onclick="changeimg("http://192.168.1.81:80/images/245.bmp",170,140)" />
</body></html>
这个运行到debugger;下面一行就写"theElement为空或者不是对象"
请问代码错到哪里了呢?谢谢
发表于:2009-10-17 13:39:42<html>
<head>
</head> <body style="margin:0px;padding:0px;">
<script type="text/javascript">
<!--
function changeimg(address,width,height)
{
var theElement = document.getElementById('capture');
theElement.setAttribute('src',address);
theElement.setAttribute('width',width);
theElement.setAttribute('height',height);
}
//-->
</script>
<img id="capture" name="capture" src="http://192.168.1.81:80/images/246.bmp" width="170" height="140" />
<input type="button" value="改变图像" onClick="changeimg('http://192.168.1.81:80/images/245.bmp',170,140)" />
</body> </html>
为什么在函数外面不行呢,
不能把theElement当做全局变量使用吗
当你调用这个函数的时候~·没有执行var theElement = document.getElementById('capture');这句~因为它写在了函数外面~·
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Attribute Test</title>
<meta http-quiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<font id="test" size="2" color="red">Change my attributes!</font>
<script type="text/javascript">
<!--
//获取id为test的结点的相关属性,并存储备用
theElement=document.getElementById('test');
//-->
</script>
<form name="testform" id="testform" action="#" method="get">
颜色:
<input type="text" id="color" name="color" value="" size="8">
<input type="button" value="设置颜色"
onclick="theElement.setAttribute('color',document.testform.color.value);">
<input type="button" value="移除颜色"
onclick="theElement.removeAttribute('color');">
<br>字号:
<select onchange="theElement.setAttribute('size',this.options[this.selectedIndex].text);">
<option>1</option>
<option>2</option>
<option selected="selected">3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
</select>
</form>
<script type="text/javascript">
<!--
//使用getAttribute()对颜色进行设置
document.testform.color.value=theElement.getAttribute('color');
//-->
</script>
</body>
</html>
那这个设置全局变量为什么就好用呢,
能不能稍微解释一下差别呢
可以,当执行document.getElementById('capture')时,body都还未加载完成,根本找不到capture.
<script type="text/javascript">
<!--
var theElement ;
function changeimg(address,width,height)
{
theElement = document.getElementById('capture');
theElement.setAttribute('src',address);
theElement.setAttribute('width',width);
theElement.setAttribute('height',height);
}
//-->
</script>