**所以,我想知道是否可以访问函数内部的内容并在 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
}
关于javascript - 是否可以通过 onclick 事件访问函数内部的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55841415/