jQuery获取被单击元素的css属性

问题描述:

我正在尝试获取所单击元素的背景色,但获得的值是:悬停.这是我的代码:

I'm trying to get the background color of the clicked element, but I get the value for: hover. This is my code:

$(button).click(function() {
  var bgColor = $(this).css('background-color');
  console.log(bgColor);
});

我在做什么错了?

您可以使用event.target

$(button).click(function(e) {
  var bgColor = $(e.target).css('background-color');
  console.log(bgColor);
});