【问题标题】:Is there away to access whats inside a function with an onclick event? [closed]是否可以通过 onclick 事件访问函数内部的内容? [关闭]
【发布时间】:2019-04-25 03:30:56
【问题描述】:

**所以,我想知道是否可以访问函数内部的内容并将其用于 onClick 事件。

例如:

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

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

    if(random number ===1) do something on onClick event.

【问题讨论】:

    标签: javascript random onclick


    【解决方案1】:

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

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

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

    if(random() == 1){
       // Random is 1
    } else {
       // Random is not 1
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-08
      • 2023-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多