JavaScript getelementsbyclassname function _javascript tips

Source: Internet
Author: User
A script to get elements based on classname on the web today is recorded here for later use.
Copy Code code as follows:

var getelementsbyclassname = function (searchclass, node, tag) {
if (docu Ment.getelementsbyclassname) {
return document.getelementsbyclassname (searchclass)
} else {
node = node || Document
Tag = Tag | | "*";
var classes = Searchclass.split (""),
elements = (Tag = = "*" && node.all)? Node.all:node.getElementsByTagName (tag),
patterns = [],
returnelements = [],
Current,
match;
var i = classes.length;
while (>= 0) {
Patterns.push (new RegExp (^|\\s) + classes[i] + (\\s|$)));
}
var j = elements.length;
while (--j >= 0) {
current = Elements[j];
match = false;
for (var k = 0, KL = patterns.length k < KL; k++) {
match = patterns[k].test (current.classname);
if (!match) break;
}
if (match) Returnelements.push (current);
}
Return returnelements;
}
}

Here are some other relevant introduction on the Internet, we can refer to it together.

The getelementsbyclassname in the DOM is interpreted as follows: The DOM API provides 3 methods for fetching elements (Getelementbyid,getelementsbyname,getelementsbytagname). Often write CSS people will naturally have questions, there is no according to the style class name element method, unfortunately, dom1/2 inside there is no such method, prototype very early extended the DOM method, added Getelementsbyclassname, from the method name to see, Seems very orthodox, and the previous 3 method names also like, analysis of its code, but found or through getelementsbytagname to achieve. This method is not elegant because it involves traversing all the elements, probing whether the element contains the target style class name, and returning an array of elements that match the criteria. Google, but did not find a more elegant and efficient alternative method.
Copy Code code as follows:

function Getelementsbyclassname (className, parentelement) {
var Elems = ($ (parentelement) | | document.body). getElementsByTagName ("*");
var result=[];
for (i=0; j=elems[i]; i++) {
if (("+j.classname+"). IndexOf ("" +classname+ ")!=-1) {
Result.push (j);
}
}
return result;
}

Since there are getelementsbyclassname, the same can have getelementsbyattribute (such as: Getelementsbyvalue, Getelementsbystyle, Getelementsbytype)
Copy Code code as follows:

Document.getelementsbyclassname = function (Classname,obox) {
Applies to getting all HTML elements within an HTML block that contain a particular classname
this.d= Obox | | Document
var children = this.d.getelementsbytagname (' * ') | | document.all;
var elements = new Array ();
for (var II = 0; II < children.length; ii++) {
var child = Children[ii];
var classnames = Child.className.split (');
for (var j = 0; J < Classnames.length; J + +) {
if (classnames[j] = = ClassName) {
Elements.push (child);
Break
}
}
}
return elements;
}

Document.getelementsbytype = function (Stypevalue,obox) {
Applies to getting all HTML elements within an HTML block that belong to a particular type, such as: Input,script,link, etc.
this.d= Obox | | Document
var children = this.d.getelementsbytagname (' * ') | | document.all;
var elements = new Array ();
for (var II = 0; II < children.length; ii++) {
if (Children[ii].type = = Stypevalue) {
Elements.push (Children[ii]);
}
}
return elements;
}

function $ () {
var elements = new Array ();
for (var II = 0; II < arguments.length; ii++) {
var element = Arguments[ii];
if (typeof element = = ' String ')
element = document.getElementById (element);
if (arguments.length = 1)
return element;
Elements.push (Element);
}
return elements;
}

$Cls = function (s,o) {
Return Document.getelementsbyclassname (S,o);
};

$Type = function (s,o) {
Return Document.getelementsbytype (S,o);
};

$Tag = function (s,o) {
This.d=o | | Document
return This.d.getelementsbytagname (s);
};

$Name = function (s) {//by Name only for the entire document and cannot be scoped
return Document.getelementsbyname (s);
};
Related Article

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.