JEa*_*gle 4 html php random jquery button
如何使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();
});
Run Code Online (Sandbox Code Playgroud)
.........
< "base_url().'images/free.png';" rel="nofollow" border='0' align='center' alt="FREE" id="button" />
Run Code Online (Sandbox Code Playgroud)
正如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]();
}
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1450 次 |
| 最近记录: |