简体   繁体   English

如何从 jQuery 中的事件监听器 function 返回值

[英]How to return a value from event listener function in jQuery

I am having difficulty returning a value from an event listener function and storing it in a variable.我很难从事件侦听器 function 返回值并将其存储在变量中。

What I did is as below, but I can not do this and I have no idea how to solve the problem.我所做的如下,但我不能这样做,我不知道如何解决这个问题。

 $(".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>

I think this is what you are looking for我想这就是你要找的

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

Now you can return it via an alert or inserting it as html into another div.现在您可以通过警报将其返回或将其作为 html 插入另一个 div。

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

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