Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在不传递事件参数的情况下从JS函数获取激发事件?_Javascript_Event Handling - Fatal编程技术网

Javascript 如何在不传递事件参数的情况下从JS函数获取激发事件?

Javascript 如何在不传递事件参数的情况下从JS函数获取激发事件?,javascript,event-handling,Javascript,Event Handling,是否可以在不传递任何事件参数的情况下从被调用的JavaScript函数获取JavaScript激发的事件? 以下是示例代码: <html> <head> <script> function myFunction() { // How I will get from here the fired event ?? } </script> </head> &

是否可以在不传递任何事件参数的情况下从被调用的JavaScript函数获取JavaScript激发的事件?

以下是示例代码:

<html>
  <head>

    <script>
      function myFunction()
      {
        // How I will get from here the fired event  ??
      }
    </script>
  </head>
  <body>

    <p id="paragraghhh" onclick="myFunction()">
       Click on this paragraph. An alert box will
       show which element triggered the event.
    </p>

  </body>
</html> 

函数myFunction()
{
//我将如何从这里得到解雇事件??
}

点击这一段。将显示一个警报框 显示触发事件的元素。

试试这个:

onclick="myFunction(event)"
JS:

或:

JS:

更好的解决方案:

document.getElementById('paragraghhh').addEventListener('click', function(){
     console.log(this);
});

在JavaScript函数中,可以引用this.event属性

比如说,

function myFunction(){
  alert(this.event.type);
}

提醒JS事件,在本例中为“单击”

只需将HTML更改为

<p id="paragraghhh" onclick="myFunction(event)">
  Click on this paragraph. An alert box will
  show which element triggered the event.
</p>

不传递事件参数的意义是什么?使用适当的事件处理程序,而不是内联JS。问题解决了@Rodik:实际上,我希望它在primefaces中实现,在
中,从
onstart
属性中,我调用了一个不支持
事件的js函数。当我传递
事件
参数时,在
警报(event.type)
中显示
未定义
。因此,我希望通过向外传递事件参数来完成此操作。在这种情况下,这将是window ObjectThank,感谢您的快速回复,但我的要求是不向外传递任何参数。。可能吗?我想获取click事件,并从这里传播。感谢您的回复,但我想在不传递任何参数的情况下执行此操作。可能吗?
document.getElementById('paragraghhh').addEventListener('click', function(){
     console.log(this);
});
function myFunction(){
  alert(this.event.type);
}
<p id="paragraghhh" onclick="myFunction(event)">
  Click on this paragraph. An alert box will
  show which element triggered the event.
</p>
function myFunction(event)
{
  // Handle event
}