简体   繁体   English

通过在javascript函数中传递类名而不使用getElementsBy *或任何lib函数来查找页面中的所有元素

[英]Find all elements in page by passing class name in javascript function without using getElementsBy* or any lib function

I am trying to find all elements in the page by passing class name in this function, I getting all elements as arrays object but I need only those elements those are having my className only. 我试图通过在此函数中传递类名称来查找页面中的所有元素,我将所有元素作为数组对象获取,但是我只需要那些仅具有我的className的元素。

    var custSearch = function (element, className) {
    var elementsArray  = [];
    // add spaces 
    var q = ' ' + className + ' ';
    (function recFind (node) {
    // Looping through all the child nodes
     for (var i = 0; i < node.childNodes.length; i++) {
      var currentNode  = node.childNodes[i];
      var currentClass = currentNode.className;

      // check if current class match with param class 
      if ((' '+currentClass+' ').indexOf(q)) {
        elementsArray.push(currentNode);
      }


      currentNode.childNodes && recFind(currentNode);
    }
   })(element);

  return elementsArray;
  }; 
custSearch(document, 'spch');

I don't want to use getElementsByClassName function but I want similar result, this above function should give me exact result but I am not finding what I am doing wrong, Can someone tell me if I am making any logical error? 我不想使用getElementsByClassName函数,但是我想要类似的结果,上面的函数应该可以给我确切的结果,但是我没有发现自己在做错什么,有人可以告诉我是否出现任何逻辑错误吗?

HTML is like this HTML就是这样

<div class="spch s2fp-h" style="display:none" id="spch"><div class="spchc" id="spchc"><div class="_o3"><div class="_AM"><span class="_CMb" id="spchl"></span><span class="button" id="spchb"><div class="_wPb"><span class="_AUb"></span><div class="_Fjd"><span class="_oXb"></span><span class="_dWb"></span></div></div></span></div><div class="_gjb"><span class="spcht" id="spchi" style="color:#777"></span><span class="spcht" id="spchf" style="color:#000"></span></div><div class="google-logo"></div></div><div class="_ypc"><div class="_zpc"></div></div></div><div class="close-button" id="spchx">×</div></div>

Try the following: 请尝试以下操作:

function myGetElementsByClassName(className) {

    var nodes = document.getElementsByTagName('*');
  var out = [];

  for( var i=0, len=nodes.length; i<len; i++) {
    if( nodes[i].classList.contains( className ) ) {
        out.push( nodes[i] );
    }
  }

  return out;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JavaScript中,是否有任何方法可以将数组的元素传递到函数中而不传递整个数组? - In JavaScript, is there any way of passing elements of an array into a function without passing in the entire array? 将动态类名传递给JavaScript函数 - Passing dynamic class name to JavaScript function JavaScript函数仅适用于具有类名称的第一个元素,而不适用于具有该类的所有元素 - JavaScript function is only working for the first element with a class name, not all elements with that class 调用所有数组元素的函数,而不将item作为参数传递 - Call function for all array elements without passing item as parameter Javascript:在页面加载时按类名获取所有元素不起作用 - Javascript: get all elements by class name on page load NOT WORKING javascript onclick函数调用传递不带引号结果的表单名称 - javascript onclick function call passing form name without quotes results javascript函数会影响具有许多元素的类,而不会影响特殊的类 - javascript function affect a class with many elements without affecting on a spesific class Javascript传递和使用该功能 - Javascript passing and using that function 如何编写一个Book的构造函数并在不使用javascript中的CLASS函数的情况下打印所有属性? - How to write a write a constructor function for a Book and print all properties WITHOUT using CLASS function in Javascript? JavaScript:类/函数和对象使用相同的名称吗? - JavaScript: Using same name for class/function and object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM