get_elements_by_class_name.js 916 B

123456789101112131415161718192021222324
  1. // Add indexOf and getElementsByClassName methods
  2. // See: http://stackoverflow.com/questions/13261506/getelementsbyclassname-in-ie8
  3. (function() {
  4. var indexOf = [].indexOf || function(prop) {
  5. for (var i = 0; i < this.length; i++) {
  6. if (this[i] === prop) return i;
  7. }
  8. return -1;
  9. };
  10. window.getElementsByClassName = function(className, context) {
  11. if (context.getElementsByClassName) return context.getElementsByClassName(className);
  12. var elems = document.querySelectorAll ? context.querySelectorAll("." + className) : (function() {
  13. var all = context.getElementsByTagName("*"),
  14. elements = [],
  15. i = 0;
  16. for (; i < all.length; i++) {
  17. if (all[i].className && (" " + all[i].className + " ").indexOf(" " + className + " ") > -1 && indexOf.call(elements,all[i]) === -1) elements.push(all[i]);
  18. }
  19. return elements;
  20. })();
  21. return elems;
  22. };
  23. })();