拖动选择单元格并合并方法

<!DOCTYPE HTML>
<HTML>
<head>
<TITLE>The document title</TITLE>
<meta charset=utf-8>
<script src="http://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></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" οnclick="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>

转载于:https://www.cnblogs.com/liangxuru/p/6233093.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Excel 中,合并单元会导致合并区域内除第一个单元外的所有内容被清空。如果你想要合并单元同时保留所有内容,可以使用一些技巧和功能来实现。 方法一:使用文本串联函数 CONCATENATE 1. 选中需要合并单元范围。 2. 在公式栏中输入如下公式:`=CONCATENATE(A1,B1,C1)`,其中 A1, B1, C1 是你想要合并单元的引用。 3. 按下回车键,将会在公式所在的单元中显示合并后的内容。 该方法会将选中的单元内容以文本形式串联起来,而不会清空其他单元的内容。 方法二:使用文本转换为列功能 1. 选中需要合并单元范围。 2. 在 Excel 菜单栏中选择 "数据" 选项卡。 3. 在 "数据工具" 组中,点击 "文本到列" 工具按钮。 4. 在弹出的 "文本向导" 对话框中,选择 "固定宽度" 选项,并点击下一步。 5. 在下一个对话框中,可以通过拖动分隔线来指定合并后的每个字段的宽度。确保每个字段都可以容纳原始单元中的内容。 6. 点击下一步,再点击完成,将会将合并单元内容拆分到相应的列中。 通过这种方式,你可以将合并单元内容拆分到不同的列中,从而保留所有的内容。 需要注意的是,以上方法只是模拟合并单元的效果,实际上并没有真正合并单元。这意味着你无法在合并后的单元上应用式、公式等操作。如果需要真正合并单元并保留所有内容,则可能需要编写 VBA 宏或使用其他高级技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值