简体   繁体   English

用Javascript绑定事件,更改元素的ID

[英]Binding events with Javascript, changing id of elements

I have this code: 我有以下代码:

$("#container").on("click","#button1",action1);

$("#container").on("click","#button2",action2);

function action1(){
    // do something
    // change button1 id to button2
    $("#button1").prop("id","button2");
}

function action2(){
    // do something
}

So when I click on button1, the action1 will change the button1 to button2. 因此,当我单击button1时,action1会将button1更改为button2。

But the problem is after changing the id, the event action2 (which only must be executed when clicking #button2) is being executed 但是问题在于更改ID后,正在执行事件action2(仅在单击#button2时才必须执行)

But the problem is after changing the id, the event action2 (which only must be executed when clicking #button2) is being executed 但是问题在于更改ID后,正在执行事件action2(仅在单击#button2时才必须执行)

The reason is that you haven't hooked the handler to the button element, you've hooked it to the container , and then told jQuery that when the event happens , it should check to see if the event travelled through an element that matches the #button1 selector during the bubbling process. 原因是您尚未将处理程序挂钩到button元素,而是将其挂钩到了容器 ,然后告诉jQuery 当事件发生时 ,它应该检查事件是否通过了与冒泡过程中的#button1选择器。 When you change button1's id to button2 , it doesn't match anymore — but it does match the other handler, so the other handler gets called. 当您将button1的id更改为button2 ,它不再匹配-但它确实与另一个处理程序匹配,因此另一个处理程序被调用。 This dynamic nature of the dispatching is what makes event delegation so useful. 调度的这种动态特性使事件委托变得如此有用。

You can "fix" this by 您可以通过以下方式“修复”此问题

  1. Not changing the id , which is an unusual thing to do. 不更改id ,这是不寻常的事情。

  2. Hooking the handler directly rather than using event delegation. 直接挂钩处理程序而不是使用事件委托。 Of course, if you have a reason for using delegation, this isn't a good option. 当然,如果您有使用委派的理由,那么这不是一个好选择。 :-) :-)

  3. Using something else, like a class, to identify the buttons in the delegated on calls. 使用别的东西,比如一类,以确定委派的按钮on的呼叫。

Note , however, that id values must be unique. 但是请注意id必须唯一。 So if you're going to change the id of button1 , you can't change it to button2 — that id is already in use. 因此,如果要更改button1id ,则不能将其更改为button2id已在使用中。 In the examples below, I've used newbutton1 instead. 在下面的示例中,我改用了newbutton1

Example of #2 (in case you didn't really need delegation): #2的示例(以防您真的不需要委派):

 $("#button1").on("click",action1); $("#button2").on("click",action2); function action1(){ // do something alert("action1, the id of the button is: " + this.id); // change button1 id to newbutton1 $("#button1").prop("id","newbutton1"); } function action2(){ // do something alert("action2, the id of the button is: " + this.id); } 
 <div id="container"> <input type="button" id="button1" value="button1"> <input type="button" id="button2" value="button2"> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

Example of #3: #3的示例:

 $("#container").on("click", ".first-button", action1); $("#container").on("click", ".second-button", action2); function action1(){ // do something alert("action1, the id of the button is: " + this.id); // change button1 id to newbutton1 $("#button1").prop("id","newbutton1"); } function action2(){ // do something alert("action2, the id of the button is: " + this.id); } 
 <div id="container"> <input type="button" id="button1" class="first-button" value="button1"> <input type="button" id="button2" class="second-button" value="button2"> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

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

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