代码之家  ›  专栏  ›  技术社区  ›  juan

“覆盖”javascript函数

  •  2
  • juan  · 技术社区  · 17 年前

    <td id="ctl00_MainMenun1" onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(this)">
    

    我有一种方法可以覆盖该函数,并让菜单执行我的一个函数,从中我可以验证我需要什么,然后调用原始函数?

    5 回复  |  直到 17 年前
        1
  •  6
  •   Mario Menger    17 年前

    在最初定义函数后重新定义它,但要在var中跟踪它,以便以后可以调用它。您实际上是在重命名原始的Menu_Key函数。

    var originalMenu_Key = Menu_Key;
    
    Menu_Key = function(t) { 
       // do your validations here
    
       if ( /* everything validated */ ) {
         originalMenu_Key (t);
       }
    };
    
        2
  •  2
  •   Aquatic    17 年前

    上述解决方案是有效的,但在一般情况下,可以以更灵活的方式进行重新定义

    var originalMenu_Key = Menu_Key;
    
    Menu_Key = function(t) { 
       // do your validations here
    
       if ( /* everything validated */ ) {
         return originalMenu_Key.apply(this,argumets);
       }
    };
    

    在这种情况下,函数签名的任何更改都不会破坏包装逻辑。

        3
  •  1
  •   kemiller2002    17 年前

        4
  •  0
  •   Sorin    17 年前

    <td id="ctl00_MainMenun1" onmouseover="Menu_HoverStatic(this)" 
    onmouseout="Menu_Unhover(this)" onkeyup="my_Function(); Menu_Key(this);">
    

    如果你想有条件地运行第二个函数,那么添加以下内容

    <td id="ctl00_MainMenun1" onmouseover="Menu_HoverStatic(this)"
    onmouseout="Menu_Unhover(this)" onkeyup="my_Function(this);">
    
    
    function my_Function(sender)
    {
      if(valid)
         Menu_Key(sender);
    }
    
        5
  •  0
  •   diadiora    17 年前

    使用js代码:

        $(document).ready(function () {
    
        $("td").mouseover(function(){
              alert('MouseOver validation content goes here.');
            }).mouseout(function(){
              alert('MouseOut  validation content goes here.');
    });
    

    嗨!