[英]How can I run a random function when a button is clicked?
I have a popup window when clicked should display a random person's info out of 40 possible outcome.单击时我有一个弹出窗口,应显示 40 个可能结果中的随机人员信息。 I tried using the switch statement:
我尝试使用 switch 语句:
$("#ball").click(function){
var n = Math.floor(Math.random()*2);
switch(n) {
case 0:
function(){
document.getElementById("popupAvatar").innerHTML = "<img src='images/catalog/22Josephine.png' alt='' />";
document.getElementById("popupInfo").innerHTML = '<h1>Josephine</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>';
}
break;
case 1:
function(){
document.getElementById("popupAvatar").innerHTML = "<img src='images/catalog/22Josephine.png' alt='' />";
document.getElementById("popupInfo").innerHTML = '<h1>TWO</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>';
}
break;
case 2:
function(){
document.getElementById("popupAvatar").innerHTML = "<img src='images/catalog/22Josephine.png' alt='' />";
document.getElementById("popupInfo").innerHTML = '<h1>Josephine</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>';
}
break;
But it doesn't seem to work.但它似乎不起作用。
I tried jQuery, with different functions assigned to different variables, but not sure how to call a random one when the button is clicked.我尝试了 jQuery,将不同的函数分配给不同的变量,但不确定如何在单击按钮时调用随机函数。
var gacha1 = function(){
$(".gachaPopup .avatar").html("<img src='images/catalog/22Josephine.png' alt='' />");
$(".artistinfo .boxed").html('<h1>Josephine</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>');
}
var gacha2 = function(){
$(".gachaPopup .avatar").html("<img src='images/catalog/21Emily.png' alt='' />");
$(".artistinfo .boxed").html('<h1>Emily</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>');
}
var gacha3 = function(){
$(".gachaPopup .avatar").html("<img src='images/catalog/20Erica.png' alt='' />");
$(".artistinfo .boxed").html('<h1>Erica</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>');
}
$("#ball").click(gacha2);
Any help appreciated, thank you so much.任何帮助表示赞赏,非常感谢。
There are efficient ways to do this.有一些有效的方法可以做到这一点。 Rather than making a switch case and increasing your lines of code, a better approach would be to have an array of values you want to replace and access using the value from your random function.
与其制作 switch case 并增加代码行数,更好的方法是使用随机函数中的值来替换和访问一组值。
function randomName() { let names = ["Josephine", "TWO", "Josephine"]; let num = Math.floor(Math.random() * 2); document.getElementById("popupAvatar").innerHTML = "<img src='images/catalog/22Josephine.png' alt='' />"; document.getElementById("popupInfo").innerHTML = `<h1>${names[num]}</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>`; console.log(num); }
<button onclick="randomName()">Click Here</button> <div id="popupAvatar"></div> <div id="popupInfo"></div>
Try this试试这个
switch(n) {
case 0:
document.getElementById("popupAvatar").innerHTML = "<img src='images/catalog/22Josephine.png' alt='' />";
document.getElementById("popupInfo").innerHTML = '<h1>Josephine</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>';
break;
case 1:
document.getElementById("popupAvatar").innerHTML = "<img src='images/catalog/22Josephine.png' alt='' />";
document.getElementById("popupInfo").innerHTML = '<h1>TWO</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>';
break;
case 2:
document.getElementById("popupAvatar").innerHTML = "<img src='images/catalog/22Josephine.png' alt='' />";
document.getElementById("popupInfo").innerHTML = '<h1>Josephine</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>';
break;
If you want you should define the function first somewhere then call it in switch statements如果你想你应该先在某处定义函数然后在 switch 语句中调用它
switch(n) {
case 0:
func_1();
break;
case 1:
func_2();
break;
case 2:
func_3();
break;
You're not calling those functions, you're just defining them.你不是在调用这些函数,你只是在定义它们。 You would want to use what's called an
immediately invoked function execution
or IIFE .您可能想要使用所谓的
immediately invoked function execution
或IIFE 。 Basically it means you would wrap your functions in parenthesis so that it executes as soon as it's defined.基本上,这意味着您将函数括在括号中,以便它在定义后立即执行。
(function(){
document.getElementById("popupAvatar").innerHTML = '<img src="images/catalog/22Josephine.png" alt="" />';
document.getElementById("popupInfo").innerHTML = '<h1>Josephine</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>';
})();
But I wouldn't really recommend doing that, as your code is going to get pretty long and be full of redundancies.但是我真的不建议这样做,因为您的代码会变得很长并且充满冗余。 Instead, you can build an array of objects to logically hold your data and then randomly select an index from that.
相反,您可以构建一个对象数组来在逻辑上保存您的数据,然后从中随机选择一个索引。
let popups = [
{
avatar: '<img src="images/catalog/22Josephine.png" alt="" />',
info: '<h1>Josephine</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>'
},
{
avatar: '<img src="images/catalog/TWO.png" alt="" />',
info: '<h1>TWO</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>'
},
{
avatar: '<img src="images/catalog/Bob.png" alt="" />',
info: '<h1>Bob</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>'
}
];
let random = Math.floor(Math.random() * (popups.length + 1));
document.getElementById("popupAvatar").innerHTML = popups[random].avatar;
document.getElementById("popupInfo").innerHTML = popups[random].info;
I'm assuming that you're actual code isn't as redundant as your example, but if it is you could actually extract anything that repeats from your data so that you only have to write it once.我假设您的实际代码不像您的示例那样冗余,但如果是这样,您实际上可以从数据中提取任何重复的内容,这样您只需编写一次。
let popups = [
{
avatar: '22Josephine',
info: 'Josephine'
},
{
avatar: 'TWO',
info: 'Two'
},
{
avatar: 'BOB',
info: 'Bob'
}
];
let random = Math.floor(Math.random() * (popups.length + 1));
document.getElementById("popupAvatar").innerHTML = `<img src="images/catalog/${popups[random].avatar}" alt="" />`;
document.getElementById("popupInfo").innerHTML = `<h1>${popups[random].info}</h1><h2>"Decisions cultivate experience."</h2><a href="#">Go To Profile</a>`;
The backticks `` are template literals , they make concatenating strings and variables much cleaner.反引号 `` 是模板文字,它们使连接字符串和变量更加清晰。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.