Home  >  Article  >  Web Front-end  >  Solve the bug of inserting option in select tag innerHTML under IE (compatible with IE, FF, Opera, Chrome, Safari)_javascript skills

Solve the bug of inserting option in select tag innerHTML under IE (compatible with IE, FF, Opera, Chrome, Safari)_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:27:211026browse

Foreword:
This is an old bug, now a perfect solution is provided. Since I have always used createElement to create and add dynamic options, I have never encountered this problem. However, everyone has different coding styles. Some people like to write tags in the form of strings and insert them using innerHTML. Isn’t this a problem? In order to facilitate people with different coding styles, I have encapsulated a method to solve this bug in IE and be compatible with the five major browsers, so that everyone can use one method to implement different styles, which facilitates maintenance and management.

Bug description:
If you use innerHTML to insert the option option under IE, IE will remove the previous

Copy code The code is as follows:

 var sltObj=document.getElementById('xx');//Get the select object, here is just an example, you can get it according to your own habits
Function addOption(obj, arg) {
if (b$.type.isElement(arg)) {
if (b$.browser.isIE()) obj.add(arg);
else obj.add(arg, null);
return;
}
var str = '';
var slt = b$.parseDom(str)[0];
for (var i = 0, num = slt.length; i < num; i ) {
obj.appendChild(slt[0]);
}
};

Use :
Copy code The code is as follows:

 addOption(sltObj, '


END
That’s it. I recommend a js framework I wrote myself. The above method is integrated into the framework.
Use: b$('obj').addOption(arg);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn