简体   繁体   English

如何在此JavaScript生成的表中使表格单元格可单击?

[英]How do I make a table cell clickable in this JavaScript generated table?

How do I make a table cell click able in this JavaScript generated table? 如何在此JavaScript生成的表中单击表格单元格?

I think where I've placed the comment in the script tag is where I need to change the code! 我想我在脚本标签中放置注释的地方是我需要更改代码的地方!

This code works so far! 这段代码到目前为止工作!

function createTable(data) {
    var table = document.createElement("table");
    table.style.border = "1px solid #ffcc33";
    var thead = document.createElement("thead");
    thead.style.padding = "5px";
    var tr = document.createElement("tr");
    for (var i = 0;i < data[0].length; i++){
            var th = document.createElement("th");
            th.style.border = "2px solid #ff0000";
            var newText = document.createTextNode(data[0][i]);
            // maybe an onclick goes here!
            th.appendChild(newText);
            tr.appendChild(th);
        }
        thead.appendChild(tr);
        table.appendChild(thead);

        var tbody = document.createElement("tbody");
        for(var i=1;i<data.length;i++){
            var tr = document.createElement("tr");
            for(var j=0;j<data[i].length;j++){  
                var td = document.createElement("td");
                td.style.padding = "5px";
                td.style.border = "2px solid #00ff00";
                var newText = document.createTextNode(data[i][j]);
                td.appendChild(newText);
                tr.appendChild(td);
            }
            tbody.appendChild(tr);
            }
            table.appendChild(tbody);               return table;

  }
  window.onload = function() {
        var table = createTable ([
        ["1","2","3","4"],
        ["One","Two","Three","Four"],
        ["Un","Deux","Trois","Quatre"],
        ["eins","zwei","drei","vier"]
        ]);
        document.body.appendChild(table)
      }

Are you opposed to jquery? 你反对jquery吗?

You could try adding jquery to the header and adding 您可以尝试将jquery添加到标题并添加

$(newText).click(function(){
     //do stuff
});

Well, yes, you could hook up click on each and every th element where you've put that comment: 嗯,是的,你可以挂接click对每一个th元素,你已经把这一评论:

if (th.addEventListener) {
    // Standard
    th.addEventListener('click', clickHandler);
}
else if (th.attachEvent) {
    // IE8 and earlier
    th.attachEvent('onclick', function(e) {
        return clickHandler.call(this, e || window.event);
    });
}

...but your best bet is probably to use event delegation. ...但你最好的选择可能是使用事件委托。 Hook click on the thead element (since you only want the th s): 挂钩clickthead元素(因为你只想要th或多个):

if (thead.addEventListener) {
    // Standard
    thead.addEventListener('click', clickHandler);
}
else if (thead.attachEvent) {
    // IE8 and earlier
    thead.attachEvent('onclick', function(e) {
        return clickHandler.call(this, e || window.event);
    });
}

...and then look at the target property on the event object passed into clickHandler . ...然后查看传递给clickHandler的事件对象的target属性。 That will be the element on which the click actually occurred (some descendant of the thead , or the thead itself). 这将是实际发生点击的元素( thead某个后代,或者thead本身)。

If you only care about th clicks, the handler might look something like this: 如果你只在乎th点击,该处理器可能是这个样子:

function clickHandler(e) {
    var target = e.target, th;
    while (target && target !== this) {
        if (target.nodeName.toUpperCase() === "TH") {
            th = target;
            break;
        }
        target = target.parentNode;
    }
    if (th) {
        // It's a th, do something
    }
}

The same technique can be very handy for handling clicks on cells in the table body. 对于处理表体中单元格的点击,相同的技术非常方便。 Obviously you capture those either on the tbody or the table itself, however you want to handle it. 显然你在tbodytable本身捕获它们,但是你想要处理它。


Note that this can be a lot simpler if you can use a decent JavaScript DOM library, such as jQuery , YUI , Closure , or any of several others . 请注意,这可能是简单了很多 ,如果你可以使用一个像样的JavaScript DOM库,如jQuery的YUI关闭 ,或任何其他几个人的

For instance, the event delegation example in jQuery: 例如,jQuery中的事件委托示例:

$(thead).on('click', 'th', function(e) {
    // It's a th, handle it
});

No IE-checking, and the delegation handling is done for you. 没有IE检查,并且为您完成了委托处理。

I think event delegating is better in case of dynamic dom. 我认为事件委托在动态dom的情况下更好。 And separating DOM and eventHandler is better too. 分离DOM和eventHandler也更好。

// or attachEvent, you have to handle corss browsers.
table.addEventListener('click', function(evt) {
  if (evt.target.tagName === 'TD') {
      alert(evt.target.innerHTML);
  }
}

so, you can add EventHander to your table(or target DOM) and then when event occur, you can check where event is occured from and choose action depends on DOM. 因此,您可以将EventHander添加到您的表(或目标DOM),然后当事件发生时,您可以检查事件发生的位置,并选择依赖于DOM的操作。

see this . 看到这个

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

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