简体   繁体   English

为什么onclick函数在onclick上运行,而在我手动调用时却不能运行?

[英]Why does the onclick function run on onclick, but not when I call it manually?

This is my code 这是我的代码

 // Register onclick
 var old_onclick = link.onclick;

 link.onclick = function() {
  astr_track_action(param);

  if(typeof(old_onclick) == "function")
   old_onclick();
 }

And this is the html 这是HTML

<a onclick="alert('hello!');" href="http://www.google.com?acme=link&foo=bar">To google!</a>

When I click the link, the alert pops up. 当我单击链接时,弹出警报。 But when I override the onclick with my JS code, the alert does not pop up. 但是,当我用JS代码覆盖onclick时, 不会弹出警报。

Any ideas? 有任何想法吗?


Edit: I just want to add, I have debugged and confirmed that old_onclick() is run, but no alert message shows up. 编辑:我只想添加,我已经调试并确认old_onclick()已运行,但是没有显示警告消息。


Edit: Here is the full code from the loop start. 编辑:这是从循环开始的完整代码。 I don't see how it's relevant, but it was requested: 我没有看到它的相关性,但有人要求:

for(var i = 0; i < document.links.length; i++)
{
    var link = document.links[i];
    var eventlink = link.href.split("acme=");

    if(eventlink.length > 1)
    {
        var param = eventlink[1].split("&")[0];
        var newlink = link.href;

        // Register onclick
        var old_onclick = link.onclick;

        link.onclick = function() {
            astr_track_action(param);

            if(typeof(old_onclick) == "function")
                old_onclick();
        }

This should work as expected: 这应该可以正常工作:

example: http://www.jsfiddle.net/8RJ5y/ 示例: http//www.jsfiddle.net/8RJ5y/

It does work, so far as I can tell: jsfiddle 据我所知,它确实有效: jsfiddle

Note that, if you are doing anything using this , you will need to use apply , rather than just invoking the function normally: 请注意,如果您正在使用this做任何事情,您将需要使用apply ,而不仅仅是正常调用该函数:

if (typeof(old_onclick) == 'function') {
    old_onclick.apply(this, arguments);
}

You are creating functions in a loop. 您正在循环创建函数。 old_onclick will point to the click handler of the last element you loop over (because upon execution, the click handlers will access old_onclick when the loop already finished). old_onclick将指向您循环的最后一个元素的单击处理程序(因为执行后,单击处理程序将在循环完成后访问old_onclick )。
You have to capture the value by eg using an immediate function: 您必须通过使用立即函数来捕获值:

var old_onclick = link.onclick;

link.onclick = (function(old_onclick) {
    return function() {
        astr_track_action(param);

        if(typeof(old_onclick) == "function")
                 old_onclick();
        }
    };
 }(old_onclick));

JavaScript has no block scope, only function scope. JavaScript没有块范围,只有函数范围。 Ie

for(...) {
    var foo = something;
}

is the same as 是相同的

var foo;
for(...) {
    foo = something;
}

暂无
暂无

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

相关问题 为什么我把 this.onclick=' '; 放在 function 运行一次? - Why does the function run once when I put this.onclick=' ';? 当我尝试在节点中运行函数时,为什么Onclick =“ function()”显示404? - why does Onclick=“function()” shows up 404 when I am trying to run a function in node? 为什么当我点击复选框时没有调用函数onclick? - Why when I click on the checkbox doesn't call the function onclick? Javascript:onclick不会调用函数 - Javascript: onclick does not call function 为什么在此 onClick function 调用中包含括号会导致它在每次呈现页面时运行,而不是在单击按钮时运行? - Why did including parenthesis in this onClick function call cause it to run every time the page rendered, but not when the button was clicked? 折射的onclick函数不会运行 - refractored onclick function does not run 为什么我的 onclick 属性在提交时不运行 function? - Why does my onclick property not run the function upon submit? 如果多次运行初始化,为什么我的jquery onclick函数会打开和关闭? - Why does my jquery onclick function toggle on and off, if I run initialization more than once? 为什么当我调用具有返回值的函数时,我的onclick事件不起作用? - Why when i call a function with returning value, my onclick event doesn't work? 当我运行与onclick属性绑定的函数时,为什么类属性未定义? - Why are my class properties undefined when i run a function which is tied to an onclick attribute?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM