如何使用js减少页面大量重复(js事件)语句
2009-05-08 17:03
666 查看
看了很多人的代码,特别是一些鼠标移进移出变色的代码块,真是一个字,惨啊
<tr onMouseOver="this.bgColor='#ff0000'" onmouseout="this.bgColor='#cccccc'">
<td>内容</td>
</tr>
<tr onMouseOver="this.bgColor='#ff0000'" onmouseout="this.bgColor='#cccccc'">
<td>内容</td>
</tr>
<tr onMouseOver="this.bgColor='#ff0000'" onmouseout="this.bgColor='#cccccc'">
<td>内容</td>
</tr>
.....
现在给你一种更方法更易维护的方法,首先输出html代码如下
<table id="onTab">
<tr>
<td>标韪栏我颜色不变</td>
</tr>
<tr rel="onMouse">
<td>内容栏我颜色想变</td>
</tr>
<tr rel="onMouse">
<td>内容栏我颜色想变</td>
</tr>
<tr rel="onMouse">
<td>内容栏我颜色想变</td>
</tr>
</table>
<script type="text/javascript">trBgColor("onTab");</script>
当显示完后,在使用js 调用 函数如 trBgColor
function trBgColor(obj){
var x = document.getElementById(obj); //只读id为obj值的内容块
var y = x.getElementsByTagName("tr"); //读tr
for (var i=0;i<y.length;i++){
if(y[i].getAttribute("rel") == "onMouse"){
//只有当tr元素 rel为 onMouse才值行以下操作,你也可以去掉这个条件
y[i].onmouseover=xxxover; //移进
y[i].onmouseout=xxxout; //移出
y[i].ondblclick=xxxdbl; //还可以加个双击事件,你也可以加onclick等等各事件
}
}
}
function xxxover(){
this.style.background="#ffc";
}
function xxxout(){
this.style.background="#fff";
}
function xxxdbl(){
alert("你双击我了");
}
<tr onMouseOver="this.bgColor='#ff0000'" onmouseout="this.bgColor='#cccccc'">
<td>内容</td>
</tr>
<tr onMouseOver="this.bgColor='#ff0000'" onmouseout="this.bgColor='#cccccc'">
<td>内容</td>
</tr>
<tr onMouseOver="this.bgColor='#ff0000'" onmouseout="this.bgColor='#cccccc'">
<td>内容</td>
</tr>
.....
现在给你一种更方法更易维护的方法,首先输出html代码如下
<table id="onTab">
<tr>
<td>标韪栏我颜色不变</td>
</tr>
<tr rel="onMouse">
<td>内容栏我颜色想变</td>
</tr>
<tr rel="onMouse">
<td>内容栏我颜色想变</td>
</tr>
<tr rel="onMouse">
<td>内容栏我颜色想变</td>
</tr>
</table>
<script type="text/javascript">trBgColor("onTab");</script>
当显示完后,在使用js 调用 函数如 trBgColor
function trBgColor(obj){
var x = document.getElementById(obj); //只读id为obj值的内容块
var y = x.getElementsByTagName("tr"); //读tr
for (var i=0;i<y.length;i++){
if(y[i].getAttribute("rel") == "onMouse"){
//只有当tr元素 rel为 onMouse才值行以下操作,你也可以去掉这个条件
y[i].onmouseover=xxxover; //移进
y[i].onmouseout=xxxout; //移出
y[i].ondblclick=xxxdbl; //还可以加个双击事件,你也可以加onclick等等各事件
}
}
}
function xxxover(){
this.style.background="#ffc";
}
function xxxout(){
this.style.background="#fff";
}
function xxxdbl(){
alert("你双击我了");
}
相关文章推荐
- 在js开发中,如何减少if else语句的使用
- 页面防止重复提交,在服务端使用struts令牌机制,前台分为jsp和extJs(其他js框架同理)
- HTML5 audio元素如何使用js与jquery控制其事件
- js 实现在离开页面时提醒未保存的信息(减少用户重复操作)
- js如何实现在监测页面关闭和刷新事件
- php大力力 [029节] 做PHP项目如何下载js文件:使用腾讯浏览器把网上案例页面存储到本地
- 使用js控制表单重复提交(1加锁,2事件方式,3 EasyUI中解决表单重复提交)
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
- JS实现60s倒计时(亲测有效),及span标签如何使用和禁用onclick事件
- 如何让iframe使用父页面的js文件,不用每次去服务器做请求
- js如何给页面中所有的img对象加上onclick事件,且实现在新窗体中查看该图片文件
- 页面内定义的js变量,如何在引入的JS文件中使用
- 如果不使用 Navigate2 的方法去访问现有页面,如何将JS写到该浏览器中???
- 如何使用V2005建立一个BHO组件--响应documentcomplete事件,去除页面中的图片
- 如何实现加载页面之前执行js语句
- onclientclick事件使用JS页面会刷新问题解决
- 使用js控制表单重复提交(1加锁,2事件方式,3 EasyUI中解决表单重复提交)
- 在一个页面重复使用一个js函数的方法
- 如何使用Oracle查询并删除重复记录的SQL语句
- A标签上使用onclick事件,js函数响应成功,单会刷新当前页面陷阱