Home >Web Front-end >JS Tutorial >Solution to innerHTML and getElementsByName bug in IE_javascript skills

Solution to innerHTML and getElementsByName bug in IE_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:30:111029browse

For example, there is such a problem.

Copy code The code is as follows:




In this div, add a playlist. This list is added using innerHTML.
Copy code The code is as follows:

I don’t need to worry about the complicated code in the middle. Now I want to take out the div named playing. It is easy to think of using
getElementsByName
. However, what is very strange is that it cannot be obtained under IE. getElementById works perfectly.
The following is a good solution:
Copy the code The code is as follows:

function GetElementsByName(tag, name) {
var elem = document.getElementsByTagName(tag);
var arr = [];
var index = 0;
var l = elem.length;
for(var i = 0; i < l; i )
{
var att = elem[i].getAttribute("name");
if(att == name) {
arr[index] = elem[i];
}
}
return arr;
}

Changed to this function, you can get it. Just one more parameter is needed.
I don’t know if bloggers have any other solutions. This solution is not perfect. Be sure to leave a message in the comments.
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