IE browsers do not support Getelementsbyclassname solutions

Source: Internet
Author: User
The code is as follows Copy Code
Document.getelementsbyclassname = function () {
var ttagname = "*";
if (Arguments.length > 1) {
Ttagname = arguments[1];
}
if (Arguments.length > 2) {
var pobj = arguments[2]
}
else{
var pobj = document;
}
var Objarr = Pobj.getelementsbytagname (ttagname);
var trobj = new Array ();
for (var i=0; i<objarr.length; i++) {
if (Objarr[i].classname = = Arguments[0]) {
Trobj.push (Objarr[i]);
}
}
return trobj;
}

Test Culvert number

The code is as follows Copy Code

function Test () {
var obj=document.getelementsbyclassname ("QQ");
for (Var i=0;i<obj.length;i++) {
Obj[i].style.color= "#ff0000 ″;
}
}

function Test2 () {
var obj=document.getelementsbyclassname ("QQ", "DIV");
for (Var i=0;i<obj.length;i++) {
Obj[i].style.color= "#ff0000 ″;
}
}

function Test3 () {
var Myid=document.getelementbyid (' OK ');
var obj=document.getelementsbyclassname ("QQ", "DIV", myID);
for (Var i=0;i<obj.length;i++) {
Obj[i].style.color= "#ff0000 ″;
}
}

Example 2

The code is as follows Copy Code

if (navigator.appname = = ' Microsoft Internet Explorer ') {
Document.getelementsbyclassname = function () {
var ttagname = "*";
if (Arguments.length > 1) {
Ttagname = arguments[1];
}
if (Arguments.length > 2) {
var pobj = arguments[2]
} else {
var pobj = document;
}
var Objarr = Pobj.getelementsbytagname (ttagname);
var trobj = new Array ();
for (var i = 0; i < objarr.length; i++) {
if (Objarr[i].classname = = Arguments[0]) {
Trobj.push (Objarr[i]);
}
}
return trobj;
}
}

Full instance

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>float</title>
<style type= "Text/css" >
. Clear{clear:both;}
. Fl{float:left;}
. Fr{float:right;}
. div1{width:200px height:200px;border:1px solid #ccc; background: #F00; Filter:alpha (opacity=50); opacity:0.5;}
. div2{width:200px height:200px;border:1px solid #ccc; background: #F00; Filter:alpha (opacity=40); opacity:0.5;}
Div3{width:auto Overflow:hidden border:1px Solid #ccc background: #999; color: #fff;}
</style>
<body>
<div class= "Div1 fl" > left float </div>
<div class= "Div2 fr" > right float </div>
<div class= "Div3" >
</div>
<div class= "Div3 div5 div6" >
</div>
<div class= "div3456456 div5 div6 div8" >
</div>

<script>
function GetClass (class_name,tag_name) {
var classes;
if (document.getelementsbyclassname) {
Classes = Document.getelementsbyclassname (class_name);
for (var i = 0; i<classes.length; i++)
alert (classes[i].innerhtml);
}
return classes;
}else{
if (tag_name = = null) {tag_name = "*";}
var tags = document.getelementsbytagname (tag_name);
var classes = new Array ();
for (var i=0,j=0; i<tags.length; i++) {
var attr_class_name = "" + Tags[i].classname + ""; Plus "" Just to add a spaces to the value of the original class attribute
if (Attr_class_name.indexof ("" + Class_name + "")!=-1) {//here Plus "" is to let the search class is a separate class, to avoid the occurrence of div3, but the phenomenon of div345.
Classes[j++] = tags[i];
}
}
alert (classes);
return classes;
}
}
var div3 = getclass ("Div3");
</script>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.