单元选择合并变色示例代码


Posted in Javascript onMay 26, 2014

周末为了解答一个问题,写了一堆代码,功能基本实现,但感觉结构太差了,没太多考虑。一点都不漂亮

<!DOCTYPE HTML> 
<HTML> 
<head> 
<TITLE>The document title</TITLE> 
<meta charset=utf-8> 
<script src="lib/jquery-1.8.1.min.js" type="text/javascript"></script> 
<style type="text/css"> 
table{ 
border-spacing: 0; 
} 
td{ 
height: 50px; 
width: 50px; 
border-spacing: 0; 
} 
.hidden{ 
display: none; 
} 
</style> 
</head> <body> 
<table border="1"> 
<tr id ="r1"> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
</tr> 
<tr id ="r2"> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
</tr> 
</table> 
<p id ='log'></p> 
<button type="button" onclick="combine()">hebing</button> 
<script type="text/javascript"> 
var selectecdlog = []; 
var resultLen = 0; 
var resultBegin = 0; 
var backColor = ['red','green','blue']; // assume 3 rows 1 -red 2 - green 3 - blue 
var resultColor =0; 
$("td").mousedown(function() { 
selectecdlog = []; 
resultColor = backColor[$(this).parent().attr('id').substr(1)-1]; 
$('td').css('background-color', 'white'); 
// ID for tr tag 
//alert($(this).parent().attr('id')); 
//get the column No. 
//alert($(this).index()+1); 
$(this).css('background-color', resultColor); 
selectecdlog.push($(this).attr('id')); 
//alert($(this).attr('id')); 
$("td").mouseup(onMouseUp); 
$("td").mouseover(onMouseOver); 
}) 
function onMouseUp(){ 
/* Act on the event */ 
var len =resultLen= Math.abs(parseInt(selectecdlog[0].substr(selectecdlog[0].indexOf('c')+1)) - parseInt($(this).attr('id').substr(($(this).attr('id').indexOf('c'))+1)))+1; 
var begin =resultBegin= parseInt(selectecdlog[0].substr(selectecdlog[0].indexOf('c')+1)) >parseInt($(this).attr('id').substr(($(this).attr('id').indexOf('c'))+1)) ? parseInt($(this).attr('id').substr(($(this).attr('id').indexOf('c'))+1)):parseInt(selectecdlog[0].substr(selectecdlog[0].indexOf('c')+1)); 
for(var i =0; i<resultLen;i++) 
{ 
selectecdlog.push(selectecdlog[0].substr(0,2)+"c"+(resultBegin+i)); 
} 
selectecdlog.shift(); 
$('#log').text(selectecdlog.toString()); 
$("td").unbind('mouseover',onMouseOver); 
$("td").unbind('mouseup',onMouseUp); 
} 
function onMouseOver (argument) { 
var len =resultLen= Math.abs(parseInt(selectecdlog[0].substr(selectecdlog[0].indexOf('c')+1)) - parseInt($(this).attr('id').substr(($(this).attr('id').indexOf('c'))+1)))+1; 
var begin =resultBegin= parseInt(selectecdlog[0].substr(selectecdlog[0].indexOf('c')+1)) >parseInt($(this).attr('id').substr(($(this).attr('id').indexOf('c'))+1)) ? parseInt($(this).attr('id').substr(($(this).attr('id').indexOf('c'))+1)):parseInt(selectecdlog[0].substr(selectecdlog[0].indexOf('c')+1)); 
$('td').css('background-color', 'white'); 
for(var i =0; i<len;i++) 
{ 
$('#'+selectecdlog[0].substr(0,2)+"c"+(begin+i)).css('background-color', resultColor); 
} 
//alert( $(this).attr('id').substr(($(this).attr('id').indexOf('c'))+1)); 
//$('#'+tdId).css('background-color', 'red'); 
} 
// set id attr for each td 
$("td").each(function(index, val){ 
/* iterate through array or object */ 
$(this).attr('id', $(this).parent().attr('id')+"c"+($(this).index()+1))}); 
//combine cells 
function combine (argument) { 
// body... 
for (var m in selectecdlog) 
{ 
if(m==0) 
{ 
$('#'+selectecdlog[m]).attr('colspan', resultLen); 
$('#'+selectecdlog[m]).css('width',resultLen*50+"px"); 
//$('#'+selectecdlog[m]).css('background-color', 'bule'); 
} 
else{ 
$('#'+selectecdlog[m]).addClass('hidden'); 
} 
} 
} 
</script> 
</body> 
</HTML>
Javascript 相关文章推荐
JQuery显示隐藏DIV的方法及代码实例
Apr 16 Javascript
在JavaScript应用中使用RequireJS来实现延迟加载
Jul 01 Javascript
JavaScript中解析JSON数据的三种方法
Jul 03 Javascript
JavaScript中Textarea滚动条不能拖动的解决方法
Dec 15 Javascript
jquery实现表格中点击相应行变色功能效果【实例代码】
May 09 Javascript
基于jQuery实现的打字机效果
Jan 16 Javascript
webpack入门必知必会
Jan 16 Javascript
动态统计当前输入内容的字节、字符数的实例详解
Oct 27 Javascript
基于Vue渲染与插件的加载顺序的问题详解
Mar 05 Javascript
jQuery实现网页拼图游戏
Apr 22 jQuery
javascript 使用sleep函数的常见方法详解
Apr 26 Javascript
javascript实现一款好看的秒表计时器
Sep 05 Javascript
jquery实现的一个文章自定义分段显示功能
May 23 #Javascript
用JavaScript实现一个代码简洁、逻辑不复杂的多级树
May 23 #Javascript
ie 7/8不支持trim的属性的解决方案
May 23 #Javascript
JQuery 控制内容长度超出规定长度显示省略号
May 23 #Javascript
jquery重复提交请求的原因浅析
May 23 #Javascript
jquery仿搜索自动联想功能代码
May 23 #Javascript
jQuery 获取/设置/删除DOM元素的属性以a元素为例
May 23 #Javascript
You might like
php使用websocket示例详解
2014/03/12 PHP
PHP生成可点击刷新的验证码简单示例
2016/05/13 PHP
PHP ADODB生成HTML表格函数rs2html功能【附错误处理函数用法】
2018/05/29 PHP
Laravel5.1 框架控制器基础用法实例分析
2020/01/04 PHP
CentOS7系统搭建LAMP及更新PHP版本操作详解
2020/03/26 PHP
js实现消息滚动效果
2017/01/18 Javascript
简单实现JavaScript弹幕效果
2020/08/27 Javascript
Vue学习笔记进阶篇之单元素过度
2017/07/19 Javascript
angularJs-$http实现百度搜索时的动态下拉框示例
2018/02/27 Javascript
Vue实现active点击切换方法
2018/03/16 Javascript
vue.js使用3DES加密的方法示例
2018/05/18 Javascript
JavaScript中变量提升与函数提升经典实例分析
2018/07/26 Javascript
Vue模拟数据,实现路由进入商品详情页面的示例
2018/08/31 Javascript
微信小程序实现的日期午别医生排班表功能示例
2019/01/09 Javascript
17道题让你彻底理解JS中的类型转换
2019/08/08 Javascript
layui动态渲染生成select的option值方法
2019/09/23 Javascript
JS中循环遍历数组的四种方式总结
2021/01/23 Javascript
手写Vue2.0 数据劫持的示例
2021/03/04 Vue.js
[56:29]Secret vs Optic 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
[46:42]DOTA2-DPC中国联赛正赛 Aster vs Magma BO3 第二场 3月5日
2021/03/11 DOTA
Python实现全角半角转换的方法
2014/08/18 Python
Python基础之函数用法实例详解
2014/09/10 Python
python的keyword模块用法实例分析
2015/06/30 Python
详解python中字典的循环遍历的两种方式
2017/02/07 Python
Python爬取成语接龙类网站
2018/10/19 Python
Python实现求两个数组交集的方法示例
2019/02/23 Python
python hash每次调用结果不同的原因
2019/11/21 Python
Django基于客户端下载文件实现方法
2020/04/21 Python
Tensorflow卷积实现原理+手写python代码实现卷积教程
2020/05/22 Python
Python爬虫爬取博客实现可视化过程解析
2020/06/29 Python
Python+OpenCV图像处理——打印图片属性、设置存储路径、调用摄像头
2020/10/22 Python
德国大型和小型家用电器网上商店:Energeto
2019/05/15 全球购物
党员岗位承诺书
2014/03/25 职场文书
企业管理标语
2014/06/10 职场文书
普通党员群众路线教育实践活动心得体会
2014/11/04 职场文书
人力资源部工作计划
2019/05/14 职场文书