简体   繁体   English

如何使用 JavaScript/jQuery 在点击时更改特定 TD 的背景颜色?

[英]How do I change a specific TD's background color on click with JavaScript/jQuery?

I have a <td style="background-color:white"></td> .我有一个<td style="background-color:white"></td>

I want to make it so that when I click inside that td , the background-color changes to black.我想这样做,以便当我在该td内单击时,背景颜色变为黑色。 How do I accomplish this with jQuery?如何使用 jQuery 完成此任务? Is it onmousedown event or click event?是 onmousedown 事件还是 click 事件?

With normal JavaScript I tried:使用正常的 JavaScript 我试过:

<td style="background-color:white" onclick="$(this).onmousedown('background-color','black')">SomeText</td>

...but it didn't work... ......但它没有工作......

Try this...尝试这个...

jQuery jQuery

$('td').click(function() {
   $(this).css('backgroundColor', '#000');
});

...or.... ...或者....

$('table').on('click', 'td', function() {
   $(this).css('backgroundColor', '#000');
});

JavaScript JavaScript

[].forEach.call(document.getElementsByTagName('td'), function(item) { 
   item.addEventListener('click', function() {
       item.style.backgroundColor = '#000';
   }, false); 
});

...or... ...或者...

var table = document.getElementsByTagName('table')[0];

var changeStyle = function(e) {
    if (e.target.tagName == 'td') {
        e.target.style.backgroundColor = '#000';
    }
};

table.addEventListener('click', changeStyle, false);

The latter examples only binds one event handler.后面的示例仅绑定一个事件处理程序。

It may be better to add a class, so you can specify your styles in a stylesheet and not couple your presentation and behavioural layer.添加 class 可能会更好,因此您可以在样式表中指定 styles,而不是耦合您的表示层和行为层。

jQuery jQuery

$('td').click(function() {
   $(this).addClass('active');
 );

...or.... ...或者....

$('table').on('click', 'td', function() {
   $(this).addClass('active');
});

CSS CSS

td.active {
  background: #000;
}

The reason this didn't work...这不起作用的原因...

<td style="background-color:white"
     onclick="$(this).onmousedown('background-color','black')">
     SomeText
</td>

...is because there is no onmousedown() event on the jQuery object (though there is mousedown() ). ...是因为 jQuery object 上没有onmousedown()事件(尽管有mousedown() )。

The correct function is mousedown .正确的 function 是mousedown But you can just use a click .但是您可以只使用click

<table> ... </table>

<script>
    $("table tr td").click(function() {
        $(this).css("background", "#fff");
    });
</script>

See this example on jsFiddle请参阅 jsFiddle 上的此示例


It is recommended that your script is not mixed with HTML, but this is valid:建议您的脚本不要与 HTML 混合,但这是有效的:

<table>
    <tr>
        <td onclick="$(this).css('background', '#fff')">change color</td>
    </tr>
</table>

See this example on jsFiddle请参阅 jsFiddle 上的此示例

Your onclick event was trying (incorrect syntax) to bind an event on itself and it wasn't changing the element style.您的 onclick 事件正在尝试(不正确的语法)在自身上绑定一个事件并且它没有改变元素样式。


This example shows why the same script on the head doesn't work.这个例子说明了为什么头部的相同脚本不起作用。

.bind or .click will bind to existing elements, live will bind on any element, even if it is inserted afterwards. .bind.click将绑定到现有元素, live将绑定到任何元素,即使它是在之后插入的。

jQuery references jQuery 参考资料

I think jquery may be overkill here.我认为 jquery 在这里可能有点矫枉过正。 You can just use something like this.你可以使用这样的东西。

<table>
    <tr>
        <td onclick="javascript:this.style.background = '#000000';">this</td>
    </tr>
</table>

You can play with it here - http://jsfiddle.net/yVvyg/你可以在这里玩 - http://jsfiddle.net/yVvyg/

This can also bedone from the head tag这也可以从头部标签开始

loadtheclicks() {
var ar = document.querySelectorAll("td"); //could also use getElementByTagname
        for (var i=0; i< ar.length; i++) {
            ar[i].addEventListener("click", function() { this.style.background = '#000000'; }, false);
        }
}

Just call that onload and you should be gold.只需调用onload,您就应该是黄金。 I have that on jsfiddle as well - http://jsfiddle.net/2anSz/4/我在 jsfiddle 上也有这个 - http://jsfiddle.net/2anSz/4/

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

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