The recent project to JS dynamic to the element binding events, just before the use of these, by the way to learn, so Google the event, wrote the following two events one add event, one is to remove the event
/addeventlistener (), RemoveEventListener () is used to handle the action/scope of the specified and deleted event handlers: The event handler runs within the scope of the element to which it belongs//addeventlistener (event, function,capture/bubble); RemoveEventListener (event,function,capture/bubble)//parameter event as shown in the previous table, function is the functions to be executed,
Capture and bubble are two types of time patterns that are developed by the world's business, and/or simply capture is to read from the beginning of the document to the last line, and then to execute the event, and bubble first to find the specified location and then execute the event. The Capture/bubble argument is a Boolean value, and true means capture, false is bubble function addevent () {var obj = document.getElementById (
"Txtiatacity"); if (Window.addeventlistener) {//Other browser event code: Mozilla, Netscape, Firefox///Added sequence of events-order of execution//note with Addeventli
Stener add events with on, without on Obj.addeventlistener (' Focus ', function () {Test (' AA ')}, False);
else {//ie Event code adds the Add method obj.attachevent (' onfocus ', function () {Test (' AA ')}) to the original event;
The function removeevnent () {var obj = document.getElementById ("txtiatacity"); if (Window.removeeventlistener) {obj.removeeventlistener (' Focus ', function (){Test (' AA ')}, False);
else {obj.detachevent (' onfocus ', function () {Test (' AA ')});
}
}
The above method is called when the page loads to bind an event to the input, the test is added successfully, but the removal is always unsuccessful. So look for resources online
//the event added through AddEventListener () can only be removed through RemoveEventListener (). It's done. Here's another sentence:// The anonymous function added by AddEventListener () at the time of removal will not be able to remove the problem found the function () {Test (' AA ')}//is not valid because incoming AddEventListener () and RemoveEventListener () method is not the same method so the test (' AA ') is proposed to write function test (msg) {alert (msg)} rewritten as a function addevent () {var obj = Document.getelementb
Yid ("txtiatacity"); if (Window.addeventlistener) {//Other browser event code: Mozilla, Netscape, Firefox///Added sequence of events-order of execution//note with Addeventli
Stener add events with on, without on Obj.addeventlistener (' Focus ', Test (' msg ')}, False);
else {//ie Event code adds the Add method obj.attachevent (' onfocus ', Test (' msg ')}) to the original event;
The function removeevnent () {var obj = document.getElementById ("txtiatacity");
if (Window.removeeventlistener) {obj.removeeventlistener (' Focus ', Test (' msg ')}, False);
else {obj.detachevent (' onfocus ', Test (' msg ')); }
}
Then execute IE hint: Type mismatch looks like it doesn't support functions with parameters and then encapsulates the function again into a parameterless form
Execute, add success, remove or fail. Looking for a long time on the internet is almost all the success of this writing, do not know why I can not remove.
Then look at the pages that are already referencing jquery and then use jquery's bind and Unbind events to succeed once.
Alas, when the project is over, solve the problem.
The above JS dynamic elements to add, remove the event implementation is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.