将另一个javascript方法绑定到Anchor标记中的href

时间:2014-08-27 15:22:06

标签: javascript jquery html onclick

我有一个网页,在onLoad中,使用jQuery创建锚标记列表 (外部javascript)。 (href和onclick事件也在那里定义)

例如:

<a href="javascript:method1(1)" onclick="return method2(1);">Test1</a>
<a href="javascript:method1(2)" onclick="return method2(2);">Test2</a>

由于它是通过外部javascript文件创建的,我无法编辑此onLoad,也不能编辑method1的方法定义

现在我希望在medthod1完成后调用method3

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您可以编写一个包装函数来同时调用:

<head>
<script>
    function wrapperFunction(arg1, arg2){
        method2(arg1);
        method3(arg2);
    }
</script>
</head>
<body>
<a href="javascript:method1(1)" onclick="wrapperFunction(1,2);">Test1</a>
...

答案 1 :(得分:1)

您可以在外部method2创建锚点后覆盖js, 在加载所有资源后,您可以使用load标记的body事件:

function window_load()
{
     var newHandler = method2;
     method2 = function(arg){

         var result = newHandler(arg);

         // ...
         // you can call here any function or something else
         // ...

         return result;
     };
}

我试图覆盖method2,通过这种方法,您可以更改任何您想要的方法