如何将onclick事件添加到表列? [英] How can I add onclick event to a table column?

查看:105
本文介绍了如何将onclick事件添加到表列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaScript将一个onclick事件添加到表列。例如,在第一列或第二列上启用onclick事件!以下函数用于行,但是我需要为特定列编辑此功能。

I'm trying to add an onclick event to a table column using JavaScript. For example, an onclick event enabled on the first or second column! The following function is for rows, but I need to edit this function for specific columns.

function addRowHandlers() {
    var table = document.getElementById("tableId");
    var rows = table.getElementsByTagName("tr");
    for(i = 0; i < rows.length; i++) {
        var currentRow = table.rows[i];
        var createClickHandler = function (row) {
            return function () {
                var cell = row.getElementsByTagName("td")[0];
                var id = cell.innerHTML;
                alert("id:" + id);
            };
        };

        currentRow.onclick = createClickHandler(currentRow);
    }
}


推荐答案

是使用jQuery返回行和列号的代码,它必须有帮助
Jsfiddle链接

This is the code to return row and column number using jQuery, it must be helpful Jsfiddle link

$('td').on('click',function() {
                var col = $(this).parent().children().index($(this));
                var row = $(this).parent().parent().children().index($(this).parent());
                alert('Row: ' + row + ', Column: ' + col);
            });

这篇关于如何将onclick事件添加到表列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆