使用Javascript在IE6中对所选单元格进行求和 [英] Using Javascript to 'sum selected cells' in IE6
问题描述
在excel中,可以突出显示一系列单元格,并查看状态栏中的总和。
这可以在IE6中使用Javascript完成和一个HTML表格?
这里有一些代码让你开始使用jQuery。当然,有很多方法可以改进。
(编辑:)有一点需要检查,它在IE中的工作情况如何。认为您需要在 mousedown
事件处理程序中添加类似 this.onselectstart = function(){return false};
在IE中禁用文本选择,但我现在没有碰巧使用IE。)
< html>
< head>
< style type =text / css>
.sel {background-color:#99ff33; }
< / style>
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js>< / script>
< script>
$(function(){
function col(cell){
return cell.parent()。children(td)。index(cell);
}
var start = null;
函数selectTo(cell){
if(start == null)
return;
$(td ).removeClass(sel);
var stop = $(cell);
var tbl = start.closest(table);
var rs = tbl.children( tbody)。children(tr);
var r0 = rs.index(start.parent()),c0 = col(start);
var r1 = rs.index(stop.parent ()),c1 = col(stop);
var sum = 0;
for(var i = r0; i <= r1; i ++){
var cells = $(rs .get(i))。children(td);
for(var j = c0; j <= c1; j ++){
var cell = $(cells.get(j)) ;
cell.addClass(sel);
sum + = Number(cell.html());
}
}
$(#total ).html(+ sum);
}
$(table) .mousedown(function(){
return false;
});
$ b $(td)。mousedown(function(){
start = $(this);
selectTo(this);
return false;
});
$ b $(td)。mouseover(function(){
selectTo(this);
});
$ b $(td)。mouseup(function(){
selectTo(this);
start = null;
});
$ b $(body)。mouseup(function(){
start = null;
});
});
< / script>
< body>
< table>
< tr> < TD→1< / TD> < TD> 2'; / TD> < TD> 3'; / TD> < TD> 4℃; / TD> < / TR>
< tr> < TD> 2'; / TD> < TD> 4℃; / TD> < TD→6< / TD> < TD> 8 LT; / TD> < / TR>
< tr> < TD> 3'; / TD> < TD→6< / TD> < TD> 9< / TD> < TD> 12< / TD> < / TR>
< / table>
< p>所选元素的总数:< span id =total>< / span>< / p>
< / body>
< / html>
In excel it is possible to highlight a range of cells and view the 'sum' in the 'status bar'.
Can this be done in IE6 using Javascript and a HTML table ?
Here is some code to get you started, using jQuery. Of course there are many ways you could improve on it.
(EDIT: One thing to check is how well it works in IE. I think you need to add something like this.onselectstart = function() {return false};
in the mousedown
event handlers to disable text selection in IE, but I don't happen to have IE handy at the moment.)
<html>
<head>
<style type="text/css">
.sel {background-color: #99ff33; }
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script>
$(function () {
function col(cell) {
return cell.parent().children("td").index(cell);
}
var start = null;
function selectTo(cell) {
if (start == null)
return;
$("td").removeClass("sel");
var stop = $(cell);
var tbl = start.closest("table");
var rs = tbl.children("tbody").children("tr");
var r0 = rs.index(start.parent()), c0 = col(start);
var r1 = rs.index(stop.parent()), c1 = col(stop);
var sum = 0;
for (var i = r0; i <= r1; i++) {
var cells = $(rs.get(i)).children("td");
for (var j = c0; j <= c1; j++) {
var cell = $(cells.get(j));
cell.addClass("sel");
sum += Number(cell.html());
}
}
$("#total").html(""+sum);
}
$("table").mousedown(function () {
return false;
});
$("td").mousedown(function () {
start = $(this);
selectTo(this);
return false;
});
$("td").mouseover(function () {
selectTo(this);
});
$("td").mouseup(function () {
selectTo(this);
start = null;
});
$("body").mouseup(function () {
start = null;
});
});
</script>
<body>
<table>
<tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr>
<tr> <td>2</td> <td>4</td> <td>6</td> <td>8</td> </tr>
<tr> <td>3</td> <td>6</td> <td>9</td> <td>12</td> </tr>
</table>
<p>Total of selected elements: <span id="total"></span></p>
</body>
</html>
这篇关于使用Javascript在IE6中对所选单元格进行求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!