简体   繁体   English

html表格中的多个单元格选择

[英]Multiple Cells Selection in Table in html

Im trying to have a mulitple selection of cells in a table in html 我试图在HTML表格中选择多个单元格

Ex : I have a 4x4 table matrix And im trying to Select multiple cells 例如:我有一个4x4的表格矩阵,而我正在尝试选择多个单元格

Also is it possible to create a dragable selection box around the table in order to select them 也可以在表格周围创建一个可拖动的选择框以选择它们

Is this possible if so can we only use javascript and css to accomplish this or do we need other softwares also 如果可以的话,这是否可行?我们可以仅使用javascript和CSS来完成此操作,还是需要其他软件?

Thanks 谢谢

$("td").click(function() {
    $(this).addClass("Selected");
});

Then just use a css class called Selected to highlight them with your style of choice. 然后,只需使用一个名为Selected的css类,以您选择的样式突出显示它们即可。

use $(".Selected") to get all selected cells 使用$(".Selected")获取所有选定的单元格

If you insist on not using jQuery then 如果您坚持不使用jQuery,那么

var addEvent = function(event, elem, f) {
    if (elem.attachEvent) {
        elem.attachEvent("on" + event, f);
    } else {
        elem.addEventListener(event, f, false);
    }
};

var addClass = function(elem, className) {
    if (elem.className.indexOf(className) == -1) {
        elem.className += " " + className;
    } else {
        elem.className = elem.className.replace(" " + className, "");    
    }
};

var addSelected = function() {
    addClass(this, "Selected");
};

var tds = document.getElementsByTagName("td");

for (var i = 0, elem = tds[i]; i < tds.length; elem = tds[++i]) {
    addEvent("click", elem, addSelected);
}

See live example 查看现场示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM