getElementsByClassName
getElementsByClassName
wangwenya 发表于3年前
getElementsByClassName
  • 发表于 3年前
  • 阅读 76
  • 收藏 0
  • 点赞 0
  • 评论 0

华为云·免费上云实践>>>   

摘要: getElementsByClassName

var getElementsByClassName = function (className, tag, elm){

  if (document.getElementsByClassName) {

  getElementsByClassName = function (className, tag, elm) {

  elm = elm || document;

  var elements = elm.getElementsByClassName(className),

  nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,

  returnElements = [], current;

  for(var i=0, il=elements.length; i

  current = elements[i];

  if(!nodeName || nodeName.test(current.nodeName)) {

  returnElements.push(current);}}

  return returnElements;};}

  else if (document.evaluate) {getElementsByClassName = function (className, tag, elm) {

  tag = tag || "*";elm = elm || document;var classes = className.split(" "),classesToCheck = "",

  xhtmlNamespace = "http://www.w3.org/1999/xhtml",

  namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,

  returnElements = [],elements,node;

  for(var j=0, jl=classes.length; j

  classesToCheck += "[contains(concat( , @class , ), " + classes[j] + " )]";

  }try {elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);

  }catch (e) {elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);}

  while ((node = elements.iterateNext())) {

  returnElements.push(node);}return returnElements;};}

  else {getElementsByClassName = function (className, tag, elm) {

  tag = tag || "*";elm = elm || document;var classes = className.split(" "),classesToCheck = [],

  elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),current,returnElements = [],match;

  for(var k=0, kl=classes.length; k<KL; k+="1){classesToCheck.push(new" regexp(?(^|\\s)?="" classes[k]="" +="" ?(\\s|$)?));}<="" p="">

  for(var l=0, ll=elements.length; l

  for(var m=0, ml=classesToCheck.length; m

  match = classesToCheck[m].test(current.className);

  if (!match) {break;}}

  if (match) {returnElements.push(current); }}return returnElements;};}

  return getElementsByClassName(className, tag, elm);};

共有 人打赏支持
粉丝 3
博文 86
码字总数 33768
×
wangwenya
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: