gpt4 book ai didi

jquery - 将按钮 id 传递给函数

转载 作者:行者123 更新时间:2023-12-01 02:55:14 25 4
gpt4 key购买 nike

我有一个 html 按钮,我试图将其 id 传递给函数

<button class= "btn1" id="buttonid1">Set Text</button>
<button class= "btn1" id="buttonid2">Set Text</button>

这是它的 Javascript

$(".btn1").click(function(){
doSomething();
});

function doSomething() {
goUrl = 'http://www.example/'
+ $(this).attr('id');
window.location = goUrl;
}

我希望该功能根据按下的按钮将浏览器重定向到不同的网站

最佳答案

为什么要创建匿名函数来运行函数。直接传递函数即可:

$(".btn1").click(doSomething);

您不必更改 doSomething 函数。

关于jquery - 将按钮 id 传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24917440/

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