如何让一个HTML按钮的onclick事件随机触发两个不同函数中的一个?

时间:2022-09-25 21:18:26

How do I make an HTML button’s onclick event trigger one of two different functions at random?

如何让一个HTML按钮的onclick事件随机触发两个不同函数中的一个?

I’m using PHP on the server, and jQuery on the client. Using this code when i click the button image nothing happens...

我在服务器上使用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"   />

5 个解决方案

#1


0  

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

#2


5  

As Jon said, attach one function to the button’s onclick event, then have that function call one of your two functions randomly.

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

In jQuery, you could do it like this:

在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]();
    }
);

#3


3  

Say you have code like:

比如你有这样的代码:

$('#button').click(function() {
    // handler 1 code
});

$('#button').click(function() {
    // handler 2 code
});

You would change it to:

您可以将其更改为:

$('#button').click(function() {
  if(Math.random() < 0.5) {
    // handler 1 code
  } else {
    // handler 2 code
  }
});

#4


2  

Attach one event handler, and make that single handler decide what to do randomly.

附加一个事件处理程序,并让这个单独的处理程序随机决定要做什么。

For example, in C# you might do:

例如,在c#中你可以这样做:

private readonly Random rng = new Random();
...
button.Click += TakeRandomAction;
...
private static void TakeRandomAction(object sender, EventArgs e)
{
    if (rng.Next(2) == 0)
    {
        GiveUserPony();
    }
    else
    {
        NukePlanetFromOrbit(); // It's the only way to be sure
    }
}

The details may vary in jQuery / JavaScript, but basically you'd still make onclick call a single function which then worked out what to do.

在jQuery / JavaScript中,细节可能会有所不同,但基本上您仍然会让onclick调用一个函数,然后它就会知道该怎么做了。

#5


0  

function randomFunction() {
    var args = arguments;
    return function() {
        args[Math.floor(Math.random()*args.length)]();
    };
}

$('#id').click(randomFunction(functionOne, functionTwo));

Haven't tested, hope it works. How it works: randomFunction returns a function which itself calls one of the arguments to randomFunction randomly.

还没有测试,希望它能工作。工作原理:randomFunction返回一个函数,该函数本身调用随机性函数的一个参数。

This approach obviously only makes sense if you have several events, there you want a random function to respond to them. If it's only this single event, than using one of very versions above is simpler.

显然,这种方法只有在有多个事件时才有意义,您需要一个随机函数来响应它们。如果它只是一个事件,那么使用上面的一个版本会更简单。

#1


0  

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

#2


5  

As Jon said, attach one function to the button’s onclick event, then have that function call one of your two functions randomly.

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

In jQuery, you could do it like this:

在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]();
    }
);

#3


3  

Say you have code like:

比如你有这样的代码:

$('#button').click(function() {
    // handler 1 code
});

$('#button').click(function() {
    // handler 2 code
});

You would change it to:

您可以将其更改为:

$('#button').click(function() {
  if(Math.random() < 0.5) {
    // handler 1 code
  } else {
    // handler 2 code
  }
});

#4


2  

Attach one event handler, and make that single handler decide what to do randomly.

附加一个事件处理程序,并让这个单独的处理程序随机决定要做什么。

For example, in C# you might do:

例如,在c#中你可以这样做:

private readonly Random rng = new Random();
...
button.Click += TakeRandomAction;
...
private static void TakeRandomAction(object sender, EventArgs e)
{
    if (rng.Next(2) == 0)
    {
        GiveUserPony();
    }
    else
    {
        NukePlanetFromOrbit(); // It's the only way to be sure
    }
}

The details may vary in jQuery / JavaScript, but basically you'd still make onclick call a single function which then worked out what to do.

在jQuery / JavaScript中,细节可能会有所不同,但基本上您仍然会让onclick调用一个函数,然后它就会知道该怎么做了。

#5


0  

function randomFunction() {
    var args = arguments;
    return function() {
        args[Math.floor(Math.random()*args.length)]();
    };
}

$('#id').click(randomFunction(functionOne, functionTwo));

Haven't tested, hope it works. How it works: randomFunction returns a function which itself calls one of the arguments to randomFunction randomly.

还没有测试,希望它能工作。工作原理:randomFunction返回一个函数,该函数本身调用随机性函数的一个参数。

This approach obviously only makes sense if you have several events, there you want a random function to respond to them. If it's only this single event, than using one of very versions above is simpler.

显然,这种方法只有在有多个事件时才有意义,您需要一个随机函数来响应它们。如果它只是一个事件,那么使用上面的一个版本会更简单。