是否可以通过onclick事件访问函数内部的内容? [关闭]

问题描述 投票:0回答:1

**所以,我想知道是否有可能访问函数内部的内容并在onClick事件中使用它。

例如:

   function random(){
       Math.floor(Math.random()*2+1)
   }

我的问题是;我想在函数内部使用If语句。

    if(random number ===1) do something on onClick event.
javascript random onclick
1个回答
1
投票

使用return关键字来说明函数的值是什么:

function random(){
   return Math.floor(Math.random()*2+1)
}

然后,其他代码可以通过执行以下操作来访问该值:random()。例如:

if(random() == 1){
   // Random is 1
} else {
   // Random is not 1
}
© www.soinside.com 2019 - 2024. All rights reserved.