javascript 如何调用javascript事件函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14457891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call javascript event function
提问by ZeDonDino
ow to call a javascript event from another javascript function by obtaining this event via getter.
现在通过 getter 获取此事件,从另一个 javascript 函数调用 javascript 事件。
Some example code is below:
一些示例代码如下:
<script type="text/javascript">
function keyDownHandler(elem, e)
{
var keyId = e.keyCode;
if(keyId === 13)
{
var clickMethod = elem.onclick;
//now call the clickHandler method.
}
}
</script>
<input type="text" onkeydown="keyDownHandler(this, event)"
onclick="clickHandler(this)" />
I want to invoke the method defined in onclick attribute of the input field
我想调用输入字段的 onclick 属性中定义的方法
回答by Rahul R.
As Tim S. said, you can directly call handler, however if you really want to call the event, you can use Jquery 'trigger'.
正如 Tim S. 所说,你可以直接调用 handler,但是如果你真的想调用事件,你可以使用 Jquery 'trigger'。
$(elem).trigger('click')
if elem is not jquery object.
如果 elem 不是 jquery 对象。
Edit: jquery is not required. The code should work by just adding the parenthesis in onclick call.
编辑:不需要 jquery。该代码应该通过在 onclick 调用中添加括号来工作。
function keyDownHandler(elem, e) {
var keyId = e.keyCode;
if (keyId === 13) {
elem.onclick();
}
}
clickHandler must be defined.
必须定义 clickHandler。
回答by Manish Nagar
use this example it will give you solution of your question
使用这个例子它会给你你的问题的解决方案
<html>
<head>
<title>Testing</title>
</head>
<body>
<script type="text/javascript">
function keyDownHandler(elem, e)
{
var keyId = e.keyCode;
if(keyId == 13)
{
// var clickMethod = elem.onclick;
clickHandler(elem, e);
}
}
function clickHandler(elem, e)
{
alert("clickHandler");
}
</script>
<input type="text" onkeydown="keyDownHandler(this, event)" onclick="clickHandler(this, event)" />
</body>
</html>
回答by Tim S.
Why not use
为什么不使用
<script type="text/javascript">
function keyDownHandler(elem, e)
{
var keyId = e.keyCode;
if(keyId === 13)
{
// call function, normally
clickHandler(elem, null);
}
}
</script>
I have set the event
argument null
, because, well, you can't generate a (correct!) event object.
我已经设置了event
参数null
,因为,你不能生成一个(正确的!)事件对象。
Okay after some playing around I have managed to make this:
好吧,经过一番玩耍后,我设法做到了:
<script type="text/javascript">
function keydown_handler(el, e) {
var click, code = e.keyCode;
if(code === 13) {
var handler = el.onclick;
if(typeof handler === 'function') {
handler.apply(this, el, null);
}
}
}
function click_handler(el, e) {
alert('clicked!');
}
</script>
<input type="text" onkeydown="keydown_handler(this, event);" onclick="click_handler(this, event);" />
It seems to work, but I can't verify it works in all browsers at this point.
它似乎有效,但目前我无法验证它是否适用于所有浏览器。
回答by Arun P Johny
I wouldn't recommend that.
我不会推荐那个。
You need to abstract the shared login out of the onclick
handler as a separate method and then call the method from both the event handlers.
您需要从onclick
处理程序中抽象出共享登录作为一个单独的方法,然后从两个事件处理程序中调用该方法。
function sharedloginc(some arguments...) {
//do something
}
function keyDownHandler(el, e) {
//do something
sharedloginc(....)
}
function clickHandler(el, e){
sharedloginc(....)
}