The way of the search for the project in the classification is dynamically generated by JS, each generated elements to dynamically add properties, events. The addition of attributes can be assigned in a way that is applicable to both IE and ff. Like what:
var element = document.createelement (' select ');
Element.id = "Myselect";
The above statement will have the same effect in IE and FF, and it works fine. But most of the elements we create are going to add events to them dynamically, and obviously we can't just hit a dot in the back, just like adding a property, and then write an event name, followed by a string of code, that would be an error. So we can add the event by using the following methods:
First of all: we want to determine what browser is the current browser, we still continue to the previous definition,
if (element.attachevent) {
For IE and for IE kernel browsers (1)
}else if (element.addeventlistener) {
Browsers for FF and NS Kernels (2)
}
The above if statement block is to help us complete the current browser is IE or FF judgment.
The browser is judged, and then all we have to do is register the function inside the element. Here we give a functional function that we define:
function Showelementid (ELMT) {
alert (elmt.id);
}
function is very simple, is to indicate the parameters inside the ID of the element.
If it is an IE browser, we insert the following code into the note above (1):
Element.attachevent ("onclick", function () {Showelementid (ELMT)});
If it is the FF browser, we insert the following code into the comment above (2):
var eventName = "onclick". Replace (/on (. *)/I, ' $ ');
Element.addeventlistener (Eventname,function () {Showelementid (ELMT)},false);
Because when registering an event with an element in FF, you do not need "on" in front of the event name, so replace on.
Well, if the future colleagues in the development project, encounter to use JS dynamic element to add events, you can use the above method. This will prevent users from using the FF browser when you can not use the features you have painstakingly developed.