gpt4 book ai didi

php - 如何使 HTML 按钮的 onclick 事件随机触发两个不同功能之一?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:04:11 30 4
gpt4 key购买 nike

如何使 HTML 按钮的 onclick 事件随机触发两个不同功能之一?

我在服务器上使用 PHP,在客户端上使用 jQuery。当我单击按钮图像时使用此代码没有任何反应......

function a(){  
alert('A got called!');
}

function b(){
alert('B got called!');
}

$('#button').bind('click', function(){
var rnd = Math.floor(Math.random() * 2);
if(rnd)
a();
else
b();
});

…………

< "base_url().'images/free.png';" rel="nofollow" border='0' align='center' alt="FREE"  id="button"   />

最佳答案

正如 Jon 所说,将一个函数附加到按钮的 onclick 事件,然后让该函数随机调用您的两个函数之一。

在 jQuery 中,你可以这样做:

function a(){
alert('A got called!');
}

function b(){
alert('B got called!');
}

$('#your_buttons_id_attribute').click(
function(){
var functions = [a,b];
var index_of_function_to_call = Math.round(Math.random());
functions[index_of_function_to_call]();
}
);

关于php - 如何使 HTML 按钮的 onclick 事件随机触发两个不同功能之一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3324803/

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