根据另一个处理程序添加一个处理程序?
我有一个带有复杂“oninput”处理程序的标签,例如
<input id="x" type="text" name="x"
oninput="lotsofgeneratedcocde...."/>
我想添加另一个仅调用该处理程序的处理程序。我最初的想法是这会起作用:
<input id="x" type="text" name="x"
oninput="lotsofgeneratedcocde...." onfocus="this.oninput"/>
但事实并非如此。我应该做什么?谢谢。
编辑:我认为 onfocus="this.oninput" 会将引用复制到函数,这就是为什么我在调用时省略了括号。
I have a tag with a complex "oninput" handler, e.g.
<input id="x" type="text" name="x"
oninput="lotsofgeneratedcocde...."/>
I want to add another handler that simply calls that one. My initial though was that this would work:
<input id="x" type="text" name="x"
oninput="lotsofgeneratedcocde...." onfocus="this.oninput"/>
But it doesn't. What should I be doing? Thanks.
Edit: I thought that onfocus="this.oninput" would copy the reference to the function, that's why I left off the parentheses for a call.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
this.oninput()
(注意括号)应该有效:http://jsfiddle.net/9kNrW /
this.oninput()
(note parentheticals) should work:http://jsfiddle.net/9kNrW/
这能行吗?
我认为没有办法将生成的代码外包为可以从两个事件处理程序调用的正确函数......
This could work?
I assume there's no way to have the generated code be outsourced as proper functions that you could call from both event handlers...
简短回答:
使用括号:
onfocus="this.oninput();"
如果
oninput
引用this
或事件对象,则需要再加一点:说明:
如果您在代码中附加事件处理程序,则您的语法是正确的。因为你正在设置一个函数引用。即,
但是,当附加在标记中时,引号之间的代码实际上本身就是一个函数。例如,
相当于:
所以您编写的代码相当于:
Short answer:
Use parens:
onfocus="this.oninput();"
If
oninput
referencesthis
or the event object, you need to add a little more:Explanation:
If you were attaching the event handlers in code, your syntax is correct. Because you are setting a function reference. Ie,
But, when attached in the markup, the code between the quotes actually is itself a function. Eg,
Is equivalent to:
So your code as written is the equivalent of: