How do I change HTML table cell color on click

前端 未结 3 1493
野趣味
野趣味 2020-12-21 00:48

I\'m trying to change the background color of an HTML table cell when the user clicks on the cell. Any ideas on how to do this? I have access to the JS Prototype library, so

3条回答
  •  不思量自难忘°
    2020-12-21 01:28

    I'm not well versed in the Prototype framework, but here's some raw Javascript that'll do what you're looking for:

    var table = document.getElementsByTagName('table')[0];
    if(table) table.onclick = function(e) {
        var target = (e || window.event).target;
        if (target.tagName in {TD:1, TH:1})
            target.setAttribute('style', 'background-color: #F00');
    };​
    

    Test it on jsFiddle

提交回复
热议问题