简体   繁体   English

如何知道我在html表中的位置(日历)?

[英]How to know where I am in a html table (calendar)?

I have a calendar made with a html table with buttons in each cell, and when a button is clicked, I need to know the day and the name where that button is located (the coordinates in the table) to send that location to a function 我有一个用html表制成的日历,每个表中都有按钮,单击按钮时,我需要知道按钮的日期和名称(表中的坐标)才能将该位置发送给函数

在此处输入图片说明

NOTE : Each cell of the table is a button. 注意:表格的每个单元格都是一个按钮。

Working fiddle . 工作提琴

You've just to give your buttons a common class, then attach the click event to this class and get the cell and the row using cellIndex/rowIndex like : 您只需为按钮提供一个通用类,然后将click事件附加到该类上,并使用cellIndex/rowIndex像这样获取单元格和行:

var classname = document.getElementsByClassName("my-btn");

var myFunction = function() {
    var cellIndex = this.parentNode.cellIndex;
    var rowIndex = this.parentNode.parentNode.rowIndex;

    alert("cellIndex : "+ cellIndex + " / rowIndex : "+rowIndex);
};

for (var i = 0; i < classname.length; i++) {
    classname[i].addEventListener('click', myFunction, false);
}

Hope this helps. 希望这可以帮助。

 var classname = document.getElementsByClassName("my-btn"); var myFunction = function() { var cellIndex = this.parentNode.cellIndex; var rowIndex = this.parentNode.parentNode.rowIndex; console.log("cellIndex : " + cellIndex + " / rowIndex : " + rowIndex); }; for (var i = 0; i < classname.length; i++) { classname[i].addEventListener('click', myFunction, false); } 
 <table border=1> <tr> <th></th> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> <th>7</th> </tr> <tr> <td>1</td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> </tr> <tr> <td>2</td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> </tr> <tr> <td>3</td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> <td><button class="my-btn">Test button</button></td> </tr> </table> 

I assume that you've created the table dynamically. 我假设您已经动态创建了表。 In the following I also handle the click of a cell, not a button, as I'd propose you that. 在下文中,我还将处理单击单元格而不是按钮的问题,因为我建议您这样做。 Of couse you can use the same methods for buttons. 当然,您可以对按钮使用相同的方法。

I just had several solutions, decide which one suits your needs the best. 我只有几种解决方案,请确定哪一种最适合您的需求。

Use the cell index (Quick'n'kinda Dirty) 使用单元格索引(Quick'n'kinda Dirty)

When creating the cells, give each one an index with a similar syntax like the following: [row-name]|[col-name]. 创建单元格时,请为每个单元格使用类似以下语法的索引:[行名] | [行名]。 That way you can extract the names when catching the click event. 这样,您可以在捕获click事件时提取名称。

1.a Create the cells with any language eg PHP 1.a用任何语言(例如PHP)创建单元格

echo "<td id=".rowname."|".colname."></td>";

1.b JS: Catch click Event 1.b JS:捕获点击事件

You could add an EventListener to every single cell. 您可以将EventListener添加到每个单元格。 You shouldn't, as this article explains: https://www.kirupa.com/html5/handling_events_for_many_elements.htm 如本文所述,您不应该这样做: https : //www.kirupa.com/html5/handling_events_for_many_elements.htm

Rather use this concept: 而是使用以下概念:

// Catch click inside the table (so either the table itself or one of the children -> cells)
document.getElementById("table").addEventListener("click", function(e) {

    // be sure that user clicked a cell, not the table
        if (e.target !== e.currentTarget) {
            var id = e.target.id;
            // split id into rowname and colname
                var names = id.split("|");
            if (names[0] != '' && names[1] != '') {
                alert("Day: " + names[1] + ", Name: " + names[0]);
            }
        }

    // stop catching the event
        e.stopPropagation();

}, false);

Pro: fast, easy to implement 优点:快速,易于实施

Con: Only for dynamic table creation, data in id / split charachter mustn't be used 缺点:仅用于动态表创建,不得使用id / split charachter中的数据

Use row- and col-index 使用行索引和列索引

Got inspired by this thread on StackOverflow: Finding the location of a TD in a table 在StackOverflow上受到此线程的启发: 在表中查找TD的位置

JS: Catch click event JS:捕获点击事件

document.getElementById("table").addEventListener("click", function(e) {

    // be sure that user clicked a cell, not the table
        if (e.target !== e.currentTarget) {
            var cell = e.target;
            // get index of cell in a row -> column
              var col = cell.cellIndex;
            // get index of the cell's parent -> row
              var row = cell.parentNode.rowIndex;
            // search for the right row
                var rows = table.childNodes(); // childNodes -> tr-nodes
                var i=0;
                var rowname = '';
                while (i < rows.length && rowname == '') {
                    if (i==row) {
                        rowname = rows[i].textContent;
                    }
                    i++;
                }
            // search for the right column
                var cols = rows[i].childNodes(); // childNodes -> td-nodes
                i=0;
                var colname = '';
                while (i < cols.length && colname == '') {
                    if (i==col) {
                        colname = cols[i].textContent;
                    }
                    i++;
                }
        }

    // stop catching the event
        e.stopPropagation();

}, false);

Pro: clean solution / no restrictions in naming the cells / no "abuse" of IDs, no dynamic creation needed 优点:干净的解决方案/命名单元格无限制/无需ID的“滥用”,无需动态创建

Con: quite long code, runtime can be expected to be longer 缺点:代码很长,运行时间可能会更长

These are the two best concepts I came up with, may they help you. 这是我提出的两个最佳概念,可能对您有帮助。 And better get rid of the buttons, if you don't really need them. 如果您不需要按钮,最好不要使用这些按钮。 Just makes it all more complicated 只是使事情变得更加复杂

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

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