代码简化

freeweb 2007-11-10 02:43:22
这几天看Css Html Javascript 分离的问题~大伙帮我看一下,这段代码有什么办法可以简化。
<script type="text/javascript">
function $(Obj){
return document.getElementById(Obj);
}

$("Serkey").onfocus=function(){
$("Serkey").value="";
}

$("Serkey").onblur=function(){
if($("Serkey").value==""||$("Serkey").value=="资料搜索"){
$("Serkey").value="资料搜索";
}
}

$("Sub").onclick=function(){
if($("Serkey").value==""||$("Serkey").value=="资料搜索"){
$("Serkey").value="";
alert("搜索关健字不能为空");
return false;
}else{
return true;
}
}

$("M1").onmouseover=function(){
this.src="../Images/Cn/Menu2_1.gif";
}
$("M1").onmouseout=function(){
this.src="../Images/Cn/Menu_1.gif";
}
$("M2").onmouseover=function(){
this.src="../Images/Cn/Menu2_3.gif";
}
$("M2").onmouseout=function(){
this.src="../Images/Cn/Menu_3.gif";
}
$("M3").onmouseover=function(){
this.src="../Images/Cn/Menu2_5.gif";
}
$("M3").onmouseout=function(){
this.src="../Images/Cn/Menu_5.gif";
}
$("M4").onmouseover=function(){
this.src="../Images/Cn/Menu2_7.gif";
}
$("M4").onmouseout=function(){
this.src="../Images/Cn/Menu_7.gif";
}
$("M5").onmouseover=function(){
this.src="../Images/Cn/Menu2_9.gif";
}
$("M5").onmouseout=function(){
this.src="../Images/Cn/Menu_9.gif";
}
$("M6").onmouseover=function(){
this.src="../Images/Cn/Menu2_11.gif";
}
$("M6").onmouseout=function(){
this.src="../Images/Cn/Menu_11.gif";
}
$("M7").onmouseover=function(){
this.src="../Images/Cn/Menu2_13.gif";
}
$("M7").onmouseout=function(){
this.src="../Images/Cn/Menu_13.gif";
}
$("M8").onmouseover=function(){
this.src="../Images/Cn/Menu2_15.gif";
}
$("M8").onmouseout=function(){
this.src="../Images/Cn/Menu_15.gif";
}
</script>

主要是红色部分有没有办法简化一下
...全文
51 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gzdiablo 2007-11-10
  • 打赏
  • 举报
回复
有时该省的就省 没事别偷懒
<script type="text/javascript">
function $(Obj){
return document.getElementById(Obj);
}


var Serkey = $("Serkey");
Serkey.onfocus=function(){this.value="";}
Serkey.onblur=function(){
if(this.value==""){
this.value="资料搜索";
}
}
var Sub = $("Sub");
Sub.onclick=function(){
if(Serkey.value==""||Serkey.value=="资料搜索"){
Serkey.value="";
alert("搜索关健字不能为空");
return false;
}else{
return true;
}
}
with($("M1"))
{
onmouseover=function(){this.src="../Images/Cn/Menu2_1.gif";}
onmouseout=function(){this.src="../Images/Cn/Menu_1.gif";}
}
with($("M2"))
{
onmouseover=function(){this.src="../Images/Cn/Menu2_3.gif";}
onmouseout=function(){this.src="../Images/Cn/Menu_3.gif";}
}
//....下略
</script>
nicholsky 2007-11-10
  • 打赏
  • 举报
回复
你那个好像有规律,那直接用for去根据规律改就行了。


for(var i=1; i<=8; i++){
$("M"+i).over="../Images/Cn/Menu2_" + parseInt((2*i)-1) + ".gif"
$("M"+i).out="../Images/Cn/Menu_" + parseInt((2*i)-1) + ".gif"
$("M"+i).onmouseover=function(){this.src=this.getAttribute("over") }
$("M"+i).onmouseout=function(){this.src=this.getAttribute("out") }
}
freeweb 2007-11-10
  • 打赏
  • 举报
回复
顶上去~这个问题主要是怎样批量给指定ID动态添加事件。这是个难点~
junp2007 2007-11-10
  • 打赏
  • 举报
回复
改代码比自己写还晕。

87,915

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧