javascript - jQuery单击多个ID并获取值
问题描述
当用户单击它时,我试图获取元素的值。这是我的代码:
<div id="heroes-selection" class="row dflex justify-content-left">
<figure id="luna" class="heroes-pic border text-danger m-3" value="heroes.luna">
<img class="animated-gif">
</figure>
<figure id="qop" class="heroes-pic border text-danger m-3" value="heroes.qop">
<img class="animated-gif">
</figure>
</div>
我尝试使用 Jquery $(this)
,结果显示为undefined
.
$(".heroes-pic").on("click", () => {
player = $(this).attr("value");
console.log(player);
});
我的目标是heroes.luna
在用户单击带有id="luna"
. 同样的id="qop"
,我需要得到值heroes.qop
。谢谢!
解决方案
this
这是因为您正在使用将参数绑定到封闭范围的 this的箭头函数。
用一个函数替换它:
$(".heroes-pic").on("click", function () {
player = $(this).attr("value");
console.log(player);
}
推荐阅读
- c++ - 我如何在 mac os 中预编译 stdc++.h
- android - 在 kotlin (android) 中使用 DataBinding 的片段?
- javascript - 无效的钩子调用reactjs
- node.js - nodejs - 如何正确地从 express 服务 vue 应用程序
- javascript - D3 函数,用于产生类似仪表的刻度:[-2, -1, 0, 0, 1, 2]
- python - 如何创建向文件添加扩展名的功能?
- json - Postman 和 JAX RS 用于发布请求
- cucumber - 在 docker 中分发黄瓜 junit5 测试
- .net - 分析 Windows 10 内核转储是否挂起(可能是由于 .Net 应用程序)
- c++ - WRL::ComPtr 导致 d3d 中的对象泄漏?