简体   繁体   English

Javascript附加到onClick事件

[英]Javascript append to onClick event

I have the following Code which I know doesn't work correctly. 我有以下代码,我知道它无法正常工作。 Yes I know how to do this in jQuery but in this case I cannot use jQuery. 是的我知道如何在jQuery中执行此操作但在这种情况下我不能使用jQuery。 Please no jQuery answers. 请不要jQuery答案。

<form>
  <input type="text" name="input1" onclick="alert('hello')">
  <input type="text" name="input2">
  <input type="text" name="input3">
</form>


<script type="text\javascript">
  window.onload = function () {
    var currentOnClick;
    for (var i = 0; i < document.forms[0].elements.length; i++) {
      currentOnClick = document.forms[0].elements[i].onclick;
      document.forms[0].elements[i].onclick = function () {
        if (currentOnClick) {
          currentOnClick();
        }
        alert("hello2");
      }
    }
  }
</script>

What I'm trying to do is iterate through the form's elements and add to the onclick function. 我想要做的是遍历表单的元素并添加到onclick函数。 But due to the fact that in my last iteration currentOnClick is null this does not run as expected. 但由于在我的上一次迭代中currentOnClick为null,因此无法按预期运行。 I want to preserve each of the elements onclick methods and play them back in the new function I'm creating. 我想保留每个元素onclick方法并在我正在创建的新函数中播放它们。

What I want: 我想要的是:

  • When input1 is clicked, alert "hello" then alert "hello2" 单击input1时,提示“hello”然后警告“hello2”

  • When Input2 is clicked, alert "hello2" 单击Input2时,提示“hello2”

  • When Input3 is clicked, alert "hello2" 单击Input3时,提示“hello2”

This helps: 这有助于:

window.onload = function () {
    for (var i = 0, element; element = document.forms[0].elements[i]; i++) {
        element.onclick = (function (onclick) {
            return function(oEvent) {
                // reference to event to pass argument properly
                oEvent  = oEvent || event;
                if (onclick)
                    onclick(oEvent);
                // new code "injection"
                alert("hello2");
            }
        })(element.onclick);
    }
}

Or this: 或这个:

window.onload = function () {
    for (var i = 0, element; element = document.forms[0].elements[i]; i++) {
        element.exonclick = element.onclick;
        element.onclick = function (oEvent) {
            if (this.exonclick) {
                this.exonclick(oEvent);
            }
            //
            alert("hello2");
        }
    }
}

Please be warned, the technique will not work if event handlers were added with DOM-Events API. 请注意,如果使用DOM-Events API添加事件处理程序,该技术将无法使用。

the way you have it, your elements all refer to the same instance of currentOnClick . 你拥有它的方式,你的元素都引用currentOnClick的相同实例。 each time you assign currentOnClick to the current element's existing onclick function, it loses the reference to the previous function. 每次将currentOnClick分配给当前元素的现有onclick函数时,它都会丢失对前一个函数的引用。 you need to create the new event handler function in a separate scope for each element. 您需要在每个元素的单独范围内创建新的事件处理函数。

function addClickProxy(element) {
    var currentOnClick = element.onclick;
    element.onclick = function() {
        if (currentOnClick) {
            currentOnClick();
        }
        alert("hello2");
    }
}

window.onload = function() {
    for (var i = 0; i < document.forms[0].elements.length; i++) {
        addClickProxy(document.forms[0].elements[i]);
    }
}

this way, there are 3 different instances of currentOnClick floating around, each sealed from the others by the function scope. 这样, currentOnClick有3个不同的实例浮动,每个实例都被功能范围封闭。

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

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