<!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" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>无标题文档</title>
<script type="text/javascript">
function size()
{
document.getElementById("test").width="500";
document.getElementById("test").height="350";
}
</script>
</head><body>
<img src="qq2.png" id="test" alt="" height="360" width="480" /><br />
<input type="button" onclick="size()" value="test" /><br />
</body>
</html>请问Size是关键字吗?为什么上面的代码没有反应啊?
奇怪的是改一下名字就可以了。。
size跟input本身的size属性冲突了吧
"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" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>无标题文档</title>
<script type="text/javascript">
function size()
{
document.getElementById("test").width="500";
document.getElementById("test").height="350";
}
</script>
</head><body>
<img src="qq2.png" id="test" alt="" height="360" width="480" /><br />
<input type="button" onclick="window.size()" value="test" /><br />
</body>
</html>
改个函数名就可以了,比如setSize()
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>无标题文档</title>
<script type="text/javascript">
function size()
{
document.getElementById("test").style.width="200";
document.getElementById("test").style.height="200";
}
</script>
</head><body>
<img src="http://www.baidu.com/img/baidu_sylogo1.gif" id="test" alt="" height="360" width="480" /><br />
<input type="button" onclick="window.size()" value="test" /><br />
</body>
</html>关键是style.width和style.height
size做方法名没问题
onclick="size()"会报错
onclick="window.size()"不会
你可以在size的前面加一下修饰,比如users_size
{
document.getElementById("test").style.width="200";
document.getElementById("test").style.height="200";
}<input type="button" onclick="setSize()" value="test" /><br />
{
document.getElementById("test").style.width="200px";
document.getElementById("test").style.height="200px";
}<input type="button" onclick="setSize()" value="test" /><br />
在html标签中直接初始化事件的时候 func的名称不能和本身的属性的名称重名 否则的话被调用的是这个属性(是不是xml规范的原因 还是内部的机制 不清楚)如果使用外部代码绑定事件 则 func的名称和该标签是否重名不冲突