菜单

js 实现支持crtl与shift键的行多选

2011年05月3日 - javascript

var lastObj = {}; //选中的当前行对象
var tableObj = {}; //表对象

//按键多选
function onTrClick(o) {
tableObj = document.getElementById(“Table1”);
var len = tableObj.rows.length;
for (var i = 1; i < len; i++) { tableObj.rows[i].rowNum = i; } if (event.ctrlKey) {//如果按下了ctrl键 if (o.style.backgroundColor == "#ffffff") { o.style.backgroundColor = '#e3e9f4'; lastObj = o; }else if (o.style.backgroundColor == "#e3e9f4") { o.style.backgroundColor = '#ffffff'; } }else if (event.shiftKey) {//如果按下了shift键 var beginNum, lastNum; if (lastObj.rowNum <= o.rowNum) { beginNum = lastObj.rowNum; lastNum = o.rowNum; }else { beginNum = o.rowNum; lastNum = lastObj.rowNum; } for (var i = 0; i < len; i++) { if (i >= beginNum && i <= lastNum) { tableObj.rows[i].style.backgroundColor = "#e3e9f4"; }else{ tableObj.rows[i].style.backgroundColor = "#ffffff"; } } lastObj = o; }else { for (var i = 1; i < len; i++) { tableObj.rows[i].style.backgroundColor = "#ffffff"; } o.style.backgroundColor = '#e3e9f4'; lastObj = o; } }

发表评论

电子邮件地址不会被公开。 必填项已用*标注