Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何选择从类“X”的节点a终止路径的所有节点集,其中只有根节点和终端节点属于类“X”?_Jquery_Jquery Selectors - Fatal编程技术网

Jquery 如何选择从类“X”的节点a终止路径的所有节点集,其中只有根节点和终端节点属于类“X”?

Jquery 如何选择从类“X”的节点a终止路径的所有节点集,其中只有根节点和终端节点属于类“X”?,jquery,jquery-selectors,Jquery,Jquery Selectors,基本上,我正在寻找一种方法来选择我的类节点的子节点,即使它们不是直接的子节点,并且在给定我的类元素的情况下,不选择任何孙子节点。 例如,给定a,它将返回b,g。给定b它将返回c,f,给定c将返回d,e,d将不返回任何东西。在不遍历DOM的情况下,我似乎找不到这样做的方法 <div class="my-class" id="a"> <div> <div class = "my-class" id="b"> <div class =

基本上,我正在寻找一种方法来选择我的类节点的子节点,即使它们不是直接的子节点,并且在给定我的类元素的情况下,不选择任何孙子节点。 例如,给定a,它将返回b,g。给定b它将返回c,f,给定c将返回d,e,d将不返回任何东西。在不遍历DOM的情况下,我似乎找不到这样做的方法

<div class="my-class" id="a">
  <div>
    <div class = "my-class" id="b">
      <div class = "my-class" id="c">
        <div class = "my-class" id="d">
        </div>
        <div class = "my-class" id="e">
        </div>
      </div>
      <div class = "my-class" id="f">
      </div>
    </div>
    <div>
      <div class = "my-class" id="g">
        <div>
          <div>
            <div class="my-class" id="h">
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
我现在所做的只是遍历DOM。如果它是我的类执行我的操作,如果它不是继续遍历树的那一部分

function traverse(element) {
  $(element).children(".my-class").trigger("my-event");
  var children = $(element).children(":not(.my-class)");
  for (var i = 0; i < children.length; i++) {
    traverse(children[i]);
  }
}
尝试:

将为选择器提供直接后代

.children方法与.find方法的不同之处在于.children只沿DOM树向下移动一个级别,而.find可以向下移动多个级别以选择后代元素、孙子辈等


从指定的$root递归检查DOM树中的每个路径。var类型是可以放入jQuery-in方法中的任何内容,该方法包括选择器

$.extend({findKids: function($el, type)
    {

        //define our recursive function
        var findPaths = function($el, $root, type)
        {
            //terminating step:
            //if the node is not the root, 
            //and is a type of "type", add 
            //it to the result set and return
            if ($el.is(type) && !$el.is($root))
            {
                $foundNodes = $foundNodes.add($el);
                return 1;
            }
            else
            {
                //recursive step:
                //look at the first generation of the element's children
                $el.children().each(function() {
                    //check in child to see if its of "type" or not
                    findPaths($(this), $root, type);
                });
            }
        }
        //store everything we find:
        $foundNodes = $();

        //call it once
        findPaths($el, $el, type);
        //found nodes will be in foundNodes
        return $foundNodes;
    }});
结果


抱歉,代码示例未显示。孩子不是我所期待的,因为他们不一定是直接的孩子。你说他们不一定是直接的孩子是什么意思?参见代码示例。给定a,b和g将被选中,即使g不是直接的子项。给定a,h不应该包含在结果中吗?因为它是g的后代,不是a。我的类孙子。而且,d和e总是。我的类孙子,不管你从哪个元素开始。如果不实现:scope伪类,则单独使用选择器将始终阻止它们匹配,即使给定了c,在这种情况下,您可以使用:scope>.my类强制它们匹配。我怀疑DOM遍历是你在这里的最佳选择。是的,我有点想我必须在这里进行DOM遍历,只是希望我错过了一些选择器什么的。
$.extend({findKids: function($el, type)
    {

        //define our recursive function
        var findPaths = function($el, $root, type)
        {
            //terminating step:
            //if the node is not the root, 
            //and is a type of "type", add 
            //it to the result set and return
            if ($el.is(type) && !$el.is($root))
            {
                $foundNodes = $foundNodes.add($el);
                return 1;
            }
            else
            {
                //recursive step:
                //look at the first generation of the element's children
                $el.children().each(function() {
                    //check in child to see if its of "type" or not
                    findPaths($(this), $root, type);
                });
            }
        }
        //store everything we find:
        $foundNodes = $();

        //call it once
        findPaths($el, $el, type);
        //found nodes will be in foundNodes
        return $foundNodes;
    }});
console.log($.findKids($('#a'), '.my-class')); //#b, #g
console.log($.findKids($('#b'), '.my-class')); //#c, #f
console.log($.findKids($('#c'), '.my-class')); //#d, #e
console.log($.findKids($('#d'), '.my-class')); //empty