gpt4 book ai didi

javascript - 将单击的按钮的 ID 保存在整数变量中

转载 作者:行者123 更新时间:2023-12-03 05:15:45 25 4
gpt4 key购买 nike

我身边有几个按钮,它们都有名为“1”-“10”的 ID。我有一个在单击按钮时调用的函数,我想让这个函数读取按钮 ID 并将其保存为整数变量。这是我到目前为止所拥有的:

编辑:我根据答案中的建议更新了我的代码。到目前为止还没有成功。它不会提醒我 ID。

JS/JQUERY

var transform = function(obj){
var id = obj.id;
alert(id);
}

HTML

<a href="JavaScript: transform(this)" id="b1">

<div class = "test" > </div>
</a>

CSS

 .test {
position: absolute;
width: 20%;
height: 10%;
background-color: black;

}

最佳答案

您需要一个点击事件 - hrefonclick 事件不同。理想情况下,您可以向所有这些链接添加一个公共(public)类,然后创建 1 个简单的处理程序:

<a class="link" id="1">
<div class="h1">
<img src="https://placehold.it/256" />
</div>
</a>

还有 jQuery

$(".link").click(function(e) {
console.log(+this.id);
});

关于javascript - 将单击的按钮的 ID 保存在整数变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41615353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com