日期:2014-05-16  浏览次数:20316 次

让HTML再干净一点!
<div   id= "title1 "   class= "button1 "   onmouseover= "doMouseOver(1) "   onmouseover= "doMouseOut(1) "   onclick= "doClick(1) "> </div>
<div   id= "title2 "   class= "button1 "   onmouseover= "doMouseOver(2) "   onmouseover= "doMouseOut(2) "   onclick= "doClick(2) "> </div>
<div   id= "title3 "   class= "button1 "   onmouseover= "doMouseOver(3) "   onmouseover= "doMouseOut(3) "   onclick= "doClick(3) "> </div>
<div   id= "title4 "   class= "button1 "   onmouseover= "doMouseOver(4) "   onmouseover= "doMouseOut(4) "   onclick= "doClick(4) "> </div>
<div   id= "title5 "   class= "button1 "   onmouseover= "doMouseOver(5) "   onmouseover= "doMouseOut(5) "   onclick= "doClick(5) "> </div>

有什么方法可以不用写这么多onmouse事件在HTML里,只在JS里处理,让HTML再干净一点

------解决方案--------------------
<div id= "title1 " class= "button1 "> 111 </div>
<div id= "title2 " class= "button1 "> 222 </div>
<div id= "title3 " class= "button1 "> 333 </div>
<div id= "title4 " class= "button1 "> 444 </div>
<div id= "title5 " class= "button1 "> 555 </div>
<script language=javascript>
function doMouseOver(str){
document.getElementById( "span1 ").innerHTML=str+ ":doMouseOver "
}
function doMouseOut(str){
document.getElementById( "span1 ").innerHTML=str+ ":doMouseOut "
}
function doClick(str){
document.getElementById( "span1 ").innerHTML=str+ ":doClick "
}
var objs=document.getElementsByTagName( "div ")
for(var i=0;i <objs.length;i++)
{
var id=objs[i].id.replace( "title ", " ")
objs[i].onmouseover=new Function( "doMouseOver( "+id+ ") ")
objs[i].onmouseout=new Function( "doMouseOut( "+id+ ") ")
objs[i].onclick=new Function( "doClick( "+id+ ") ")
}
</script>
<span id= "span1 "> </span>
------解决方案--------------------
js中给控件加事件,
ie addAttachEvent()
ff addLisenterEvent()

好像是
------解决方案--------------------
window.attachEvent( 'onload ', f)....

f = function () {

s = document.getElementsByTagName( "div ")....

for (i = 0 ; i < s.length ; i ++) if (/^table\d+$/.test(s[i].id)) {
s[i].onmouseover = new Function....
s[i].onmouseout = new Function....
}

}

大概就这个意思。。
------解决方案--------------------
<div id= "title1 " class= "button1 "> 1 </div>
<div id= "title2 " class= "button1 " "> 2 </div>
<div id= "title3 " class= "button1 "> 3 </div>
<div id= "title4 " class= "button1 "> 4 </div>
<div id= "title5 " class= "button1 "> 5 </div>
<script>
for(var i=1;i <=5;i++){
var obj = document.getElementById( "title &qu