如题,下面代码是可以的但是加上
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
标准就不行了,请高手帮助解决一下/不要说原理/只要一个HTML文件就行/谢谢(禁止灌水)<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<input type="button" value="sd" onclick="doThis();"/><br/>
<div id="111" style="position:absolute;width:50px;height:50px;background-color:#FB430D;">
</div>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<input type="button" value="sd" onclick="doThis();"/>
<SCRIPT LANGUAGE="JavaScript">
function doThis(){
var a = document.getElementById("111");
a.style.left=(document.body.clientWidth/2-a.clientWidth/2)+"px";
a.style.top=(document.body.scrollTop+document.body.clientHeight/2-a.clientHeight/2)+"px";
}
</SCRIPT>
</body>
</html>
window.resize = doThis();
错了。window.onresize = doThis();
<!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 ">
<body>
<input type= "button " value= "sd " onclick= "doThis() "/> <br/>
<div id= "111 " style= "position:absolute;width:50px;height:50px;background-color:#FB430D; ">
</div>
<br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/>
<br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/>
<br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/>
<br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/>
<input type= "button " value= "sd " onclick= "doThis();"/>
<SCRIPT LANGUAGE= "JavaScript">
function doThis(){
var a = document.getElementById( "111 ");
a.style.left=(document.documentElement.clientWidth/2-a.clientWidth/2)+ "px ";
a.style.top=(document.documentElement.scrollTop+document.documentElement.clientHeight/2-a.clientHeight/2)+ "px ";
}
</SCRIPT>
</body>
</html>
例:
var top = document.documentElement.scrollTop || document.body.scrollTop;
在javascript里||是个好东西,除了能用在if等条件判断里,还能用在变量赋值上。那么上例等同于下例。
例:
var top = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
这么写可以得到很好的兼容性。