javascript 写到body中可以,写到head中为什么不可以呢?
<html><head><title></title><script>/*为什么放到这里就不可以?*/</script></head><body><form><input...
<html>
<head>
<title></title>
<script>
/*为什么放到这里就不可以?*/
</script>
</head>
<body>
<form>
<input type="text" value="输入网址" name="website" id="ipt">
<input type="button" onClick="windowopen()" value="全屏显示" name="button">
</form>
<script type="text/javascript">
var st = document.getElementById("ipt");
st.onmouseover = function(){
this.value="";
this.focus();
};
st.onblur = function(){
this.value="输入网址";
};
function windowopen(){
var target = "http://"+st.value;
newwindow = window.open("","","scrollbars");
if(document.all){
newwindow.moveTo(0,0);
newwindow.resizeTo(screen.width,screen.height);
}
newwindow.location = target;
}
</script>
</body>
</html> 展开
<head>
<title></title>
<script>
/*为什么放到这里就不可以?*/
</script>
</head>
<body>
<form>
<input type="text" value="输入网址" name="website" id="ipt">
<input type="button" onClick="windowopen()" value="全屏显示" name="button">
</form>
<script type="text/javascript">
var st = document.getElementById("ipt");
st.onmouseover = function(){
this.value="";
this.focus();
};
st.onblur = function(){
this.value="输入网址";
};
function windowopen(){
var target = "http://"+st.value;
newwindow = window.open("","","scrollbars");
if(document.all){
newwindow.moveTo(0,0);
newwindow.resizeTo(screen.width,screen.height);
}
newwindow.location = target;
}
</script>
</body>
</html> 展开
1个回答
展开全部
页面加载的时候会先加载head中的css和js,你在js的全局定义里面去取控件,在执行head的时候你body中的这个控件还没有,所以会有问题
追问
head中加上window.onload也不可以
追答
<html>
<head>
<title></title>
<script>
var st;
window.onload = function(){
st = document.getElementById("ipt");
st.onmouseover = function(){
this.value="";
this.focus();
};
st.onblur = function(){
this.value="输入网址";
};
}
</script>
</head>
<body>
<form>
<input type="text" value="输入网址" name="website" id="ipt">
<input type="button" onClick="windowopen()" value="全屏显示" name="button">
</form>
<script type="text/javascript">
function windowopen(){
var target = "http://"+st.value;
newwindow = window.open("","","scrollbars");
if(document.all){
newwindow.moveTo(0,0);
newwindow.resizeTo(screen.width,screen.height);
}
newwindow.location = target;
}
</script>
</body>
</html>
你改成这样就好了,放onload里面没什么问题..
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询