**所以,我想知道是否有可能访问函数内部的内容并在onClick事件中使用它。
例如:
function random(){
Math.floor(Math.random()*2+1)
}
我的问题是;我想在函数内部使用If语句。
if(random number ===1) do something on onClick event.
使用return
关键字来说明函数的值是什么:
function random(){
return Math.floor(Math.random()*2+1)
}
然后,其他代码可以通过执行以下操作来访问该值:random()
。例如:
if(random() == 1){
// Random is 1
} else {
// Random is not 1
}