如何从jQuery中的事件侦听器函数返回值



我很难从事件侦听器函数返回值并将其存储在变量中。

我所做的如下,但我不能这样做,我不知道如何解决这个问题。

$(".container").click(function() {
let color = $(".container").css("background-color");
//I want to return this color and store it in a variable.
})
.container {
height: 50px;
width: 50px;
background-color: red;
}
<div class="container"></div>

我想这就是您想要的

var container = document.getElementById("myDivID")
var containerColor = window.getComputedStyle(container).backgroundColor;

现在您可以通过警报返回它,或者将它作为html插入到另一个div.中

最新更新