简体   繁体   English

为什么此javascript会引发未定义的错误?

[英]Why does this javascript throw an undefined error?

I'd like to define my own onblur event for all text boxes in an application so that I can strip all high ascii values, so I wrote a script that runs on an asp.net master page that runs per page load, overriding all text box / area onblur events, and storing a copy of the old event. 我想为应用程序中的所有文本框定义自己的onblur事件,以便删除所有较高的ascii值,因此我编写了一个脚本,该脚本在asp.net主页上运行,该脚本按页面加载运行,覆盖所有文本框/区域onblur事件,并存储旧事件的副本。

The new event then called the old event so it wouldn't break existing events across the forms. 然后,新事件称为旧事件,因此它不会破坏表单中的现有事件。

It worked fine until a page defined an onblur like: onblur="func(this)" When the original event fires the 'this' point doesn't seem to point to the sender control any longer. 直到页面定义了一个onblur为止,它一直工作良好,例如:onblur =“ func(this)”当原始事件触发时,“ this”点似乎不再指向发送方控件。

Pastebin link with 2 simple examples Pastebin链接和2个简单示例

So would anyone be able to point me towards a better way to accomplish this? 那么,有谁能指出我的理想方法呢?

Thanks! 谢谢!

To call a function dynamically while controlling the value of this , use func.apply instead of a standard call. 要在控制this的值的同时动态调用函数,请使用func.apply而不是标准调用。 For example instead of 例如代替

myStoredFunc(arg1, arg2)

use : 采用 :

myStoredFunc.apply(this, arguments);

This way the value of the this variable will be correctly passed to the called function, thanks to apply 's first argument. 这样,由于apply的第一个参数,this变量的值将正确传递给被调用的函数。 The second argument allows you to specify the parameter values (here I pass all the current function's arguments to the called function). 第二个参数允许您指定参数值(这里我将当前函数的所有参数传递给被调用的函数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM