JS Native: Package Document.getelementbyclassname () function

Source: Internet
Author: User

Interface Encapsulation: Package Document.getelementbyclassname () function
function Getelementsbyclassname (cname,domtag,root) {//functions have three parameters: the first parameter is the class name (required, string form); The second argument is the parent container (optional), the body node by default The third parameter is the tag name (string form) of the DOM node
if (root) {
root = typeof Root = = "string"? document.getElementById (root): root;
} else{
root = document.body;
};
Domtag = Domtag | | "*";
var els = root.getelementsbytagname (Domtag);
var arr = [];
for (var i = 0,n = els.length; i < n; i++) {
for (var j = 0,k = Els[i].classname.split (""), L = k.length; J < L; J + +) {
if (k[j] = = CName) {
Console.log (Els[i]);//This is the label of the output DOM element object
Return Els[i];els[i] is the DOM node object, appended to the array to save
Arr.push (Els[i]);
Break
}
}
}
Return arr;//returns the array, but the array is stored in a DOM element node, on the outside to call the node, with Arr[0] This method call can be
}

JS Native: Package Document.getelementbyclassname () function

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.