»
AnglaisFran�aisVietnamien

Imprimer - getElementsByClassName: Simple Shorthand mani?re d'obtenir l'?l?ment HTML par className avec RegExp - JavaScriptBank.com

Version compl�te: jsB@nk » Snippet » getElementsByClassName: Simple Shorthand mani?re d'obtenir l'?l?ment HTML par className avec RegExp
URL: https://www.javascriptbank.com/getelementsbyclassname-simple-shorthand-way-to-get-html-element-by-classname-with-regexp.html

getElementsByClassName: Simple Shorthand mani?re d'obtenir l'?l?ment HTML par className avec RegExp © JavaScriptBank.comUne fonction simple JavaScript pour permettre aux d?veloppeurs web, les codeurs web, possibilit? pour les programmeurs obtenir des ?l?ments HTML par leurs propri?t?s className

Version compl�te: jsB@nk » Snippet » getElementsByClassName: Simple Shorthand mani?re d'obtenir l'?l?ment HTML par className avec RegExp
URL: https://www.javascriptbank.com/getelementsbyclassname-simple-shorthand-way-to-get-html-element-by-classname-with-regexp.html



JavaScript
<script type="text/javascript">// Created by: Robert Nyman | http://www.robertnyman.com// This script downloaded from www.JavaScriptBank.comfunction getElementsByClassName(className, tag, elm){var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");var tag = tag || "*";var elm = elm || document;var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);var returnElements = [];var current;var length = elements.length;for(var i=0; i<length; i++){current = elements[i];if(testClass.test(current.className)){returnElements.push(current);}}return returnElements;}</script>


HTML
<p>Some ways to call it:</p><p>To get all a elements in the document with a "info-links" class:<br><code>getElementsByClassName(document, "a", "info-links");</code></p><p>To get all div elements within the element named "container", with a "col" class:<br><code>getElementsByClassName(document.getElementById("container"), "div", "col");</code></p><p>To get all elements within in the document with a "click-me" class:<br><code>getElementsByClassName(document, "*", "click-me");</code></p>