onMouseOver不能写在<script>里吗
javascript吧
全部回复
仅看楼主
level 8
jy03692141 楼主
<script>
function func(){
document.getElementById('wd').className='bg2';
}
</script>
<div class="bg" onMouseOver="func()" id="wd"></div>
写在DIV里就完全正常
但是
<script>
function func(){
document.getElementById('wd').onMouseOver.className='bg2';
}
</script>
<div class="bg" id="wd"></div>
就不行了I
2012年03月08日 12点03分 1
level 8
新人吧?
document.getElementById('wd').onMouseOver=function func(){
this.className='bg2';
}
2012年03月08日 12点03分 2
level 8
jy03692141 楼主
是啊,我是自己看书研究的。为什么onMouseOver需要用函数封装起来啊?
2012年03月08日 13点03分 3
level 8
jy03692141 楼主
<style>
.bg{
background-image:url(1.jpg);
width:215px;
height:54px;
} .bg2{
background-image:url(2.jpg);
width:215px;
height:54px;
}
</style>
<script>
document.getElementById('wd').onMouseOver=function func(){
this.className='bg2';
}
</script>
<div class="bg" id="wd"></div>
而且鼠标经过也没反映啊I
2012年03月08日 13点03分 4
level 8
onMouseOver全小写
2012年03月08日 14点03分 5
level 8
jy03692141 楼主
没用啊也不行
2012年03月09日 03点03分 6
level 14
script标签放到div标签之后
2012年03月09日 03点03分 7
level 6
楼上正解 文档流从上向下执行 放在上面没执行到js
2012年03月09日 03点03分 8
level 8
jy03692141 楼主
<script>
function func(){
document.getElementById('wd').className='bg2';
}
</script>
<div class="bg" onMouseOver="func()" id="wd"></div>
晕倒 可是这段script就是在DIV的上面啊?请问这到底是怎么回事,到底上面时候才需要放到DIV标签后面呢?现在很迷糊啊I
2012年03月09日 04点03分 9
level 14
首先你要了解下DOM加载的次序,你就直接把script放到div的前面,执行的时候是早不到对象的[抛媚眼],要放在DIV前面的话也是放到head标签里,当然你也可以用windo w的onload函数执行I
2012年03月09日 04点03分 10
level 8
jy03692141 楼主
那是不是可以理解为所有的script都要写在DIV的下面,或者HRAD里,不然就用window.onload等加载完执行啊?
2012年03月09日 04点03分 11
level 6
这是因为
div中调用了onMouseOver方法
而你上面那个你直接写在
<script>
document.getElementById('wd').onMouseOver=function func(){
this.className='bg2';
}
</script> 这里面 所以要放在div后面
2012年03月09日 04点03分 12
level 8
声明函数并没有调用所以放哪里都一样,建议将所有代码放到body结束标签前
2012年03月09日 06点03分 13
level 9
[打酱油]
2012年03月09日 07点03分 14
level 3
函数没调用
2012年03月09日 07点03分 15
level 1
楼主你解决了吗???我也遇到这个问题了,放在window.onload里面执行还是什么?
2016年05月04日 16点05分 16
楼主现在已经是大神了[狂汗]
2016年05月04日 16点05分
回复 Y20134064 :[惊哭][惊哭][惊哭]
2016年05月04日 16点05分
回复 顺139 :挖坟也不看看时间[呵呵]
2016年05月05日 00点05分
回复 Y20134064 :有啊。反正 js 吧没吧主。回复一下也没什么的。。。[滑稽]
2016年05月05日 01点05分
level 1
<style>
*{
margin:0;padding:0;
}
.bg{
color:green;
}
.bg2{
color:pink;
}
</style>
<script>
function func(){
document.getElementById('wd').onmouseover = function(){
this.className = 'bg2';
}
}
setTimeout(func,0);
</script>
<div class="bg" id="wd">测试颜色</div>
2016年05月04日 16点05分 17
1