Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.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
Javascript jQuery-获取元素的索引,限制为类_Javascript_Jquery - Fatal编程技术网

Javascript jQuery-获取元素的索引,限制为类

Javascript jQuery-获取元素的索引,限制为类,javascript,jquery,Javascript,Jquery,给定以下html: <div class="my-container"> <div class="x"> <a href="#">Link 1</a> </div> <div class="x"> <a href="#">Link 2</a> </div> <div class="x"> &

给定以下html:

<div class="my-container">
    <div class="x">
        <a href="#">Link 1</a>
    </div>
    <div class="x">
        <a href="#">Link 2</a>
    </div>
    <div class="x">
        <a href="#">Link 3</a>
    </div>
    <div class="x y">
        <a href="#">Link 4</a>
    </div>
    <div class="x">
        <a href="#">Link 5</a>
    </div>
    <div class="x y">
        <a href="#">Link 6</a>
    </div>
    <div class="x">
        <a href="#">Link 7</a>
    </div>
</div>

哪些元素获得
y
类-是一个动态的东西,它在运行时根据不同的用户交互而变化

将鼠标移到锚点上(我可以假设锚点位于具有
y
类的div中,因为只有那些是可见的),我需要获取其容器的索引(具有
y
类的div),但仅限于该
y

意思是:

  • “链接4”上的鼠标应该告诉我:
    0
    (类为
    y
    的第一个元素)
  • “链接6”上的鼠标应该告诉我:
    1
    (第二个元素的类为
    y
  • .index()
    在这里对我没有帮助

    编辑: @凯文B
    我读过文件,但没法让它工作。我能找到的最接近的方法是将集合传递给
    .index()
    ,我已经尝试过了。但是不起作用(同样,他们的集合示例是使用vanilla js document.getElementById-这对我不起作用,需要使用类;尝试调整:myCollection=$(this).mospect('.my container').children('.y'),并将其传递到
    .index()
    ,但它不起作用)。
    我不会在没有谷歌搜索的情况下发帖子,我也会浏览文档,不知道为什么会投反对票(我不会指指点点,我不会假设我知道是谁)。就因为我说“.index()”帮不了我吗?好吧,我已经尝试了我所理解的一切,我可以用它来做,但没能实现。这就是我发布的原因。

    正如评论中所说,索引正是你所需要的:

    $(document).ready(function() {
      //mousein
      $("a").hover(function(){
         console.log($(this).index('.y a')); //-1 if elm doesnt exist
      },
      //mouseout
      function(){
    
      })
    });
    
    $(document).ready(function() {
      //mousein
      $("a").hover(function(){
         var parent = $('.my-container').eq(2); // the 3rd "my-container"
         console.log(parent.find('.y a').index(this)); //-1 if elm doesnt exist
      },
      //mouseout
      function(){
    
      })
    });
    

    正如评论中所说,索引正是您所需要的:

    $(document).ready(function() {
      //mousein
      $("a").hover(function(){
         console.log($(this).index('.y a')); //-1 if elm doesnt exist
      },
      //mouseout
      function(){
    
      })
    });
    
    $(document).ready(function() {
      //mousein
      $("a").hover(function(){
         var parent = $('.my-container').eq(2); // the 3rd "my-container"
         console.log(parent.find('.y a').index(this)); //-1 if elm doesnt exist
      },
      //mouseout
      function(){
    
      })
    });
    
    var计数=1;
    $(.my container div”).on('mouseover',function(){
    if($(this.attr(“class”).indexOf('y')>-1){
    警报(计数+y类上的第次鼠标悬停);
    计数++;
    }
    })
    
    
    var计数=1;
    $(.my container div”).on('mouseover',function(){
    if($(this.attr(“class”).indexOf('y')>-1){
    警报(计数+y类上的第次鼠标悬停);
    计数++;
    }
    })
    
    
    正如评论中所说,索引正是您所需要的:

    $(document).ready(function() {
      //mousein
      $("a").hover(function(){
         console.log($(this).index('.y a')); //-1 if elm doesnt exist
      },
      //mouseout
      function(){
    
      })
    });
    
    $(document).ready(function() {
      //mousein
      $("a").hover(function(){
         var parent = $('.my-container').eq(2); // the 3rd "my-container"
         console.log(parent.find('.y a').index(this)); //-1 if elm doesnt exist
      },
      //mouseout
      function(){
    
      })
    });
    

    正如评论中所说,索引正是您所需要的:

    $(document).ready(function() {
      //mousein
      $("a").hover(function(){
         console.log($(this).index('.y a')); //-1 if elm doesnt exist
      },
      //mouseout
      function(){
    
      })
    });
    
    $(document).ready(function() {
      //mousein
      $("a").hover(function(){
         var parent = $('.my-container').eq(2); // the 3rd "my-container"
         console.log(parent.find('.y a').index(this)); //-1 if elm doesnt exist
      },
      //mouseout
      function(){
    
      })
    });
    

    “.index()在这里对我没有帮助”你确定吗?看起来它确实可以完成任务。
    index()
    正是你所需要的:
    $(this.index('.y a')
    其中
    这是导致鼠标事件的
    a
    元素听起来像:“我需要获取索引" .. 你…吗?您可能不需要索引,除非这是一些衍生的、非真实世界的场景/技术练习。给每个容器一个“data idx=”和它所需要的实际值,并使用
    $(this).closest(“div.y”).data(“idx”)
    我已经阅读了文档,但无法使它工作。我能找到的最接近的方法是将集合传递给
    .index()
    ,我已经尝试过了。但是不起作用(同样,他们的集合示例是vanilla js document.getElementById——这对我来说不起作用;尝试进行调整,但无法使其起作用)。如果不先浏览文档和搜索,我不会发帖,我不知道为什么会投反对票。就因为我说了“.index()”对我没有帮助“?@freedomn-m-这不是理论,我为什么要浪费时间?”我有一个水平列表,在鼠标悬停在具有
    y
    类的元素上时,我需要应用一个效果,如果它是第一个
    y
    类元素,或者如果它是最后一个,效果会有所不同。所以有三种效果:第一、最后和中间的任何效果。我在这里“添加”的唯一一件事是暗示我的
    y
    类元素不一定是一个接一个的,但在实际场景中它们总是连续的。在这里做的很简单,这样我就不会被太多的html所污染。我的语义类确实比
    x
    y
    mycontainer
    多。“index()在这里帮不了我”你确定吗?当然,它似乎可以完成这项工作。
    index()
    正是您需要的:
    $(this)。index('.y a')
    其中
    this
    是导致鼠标事件的
    元素,听起来像是:“我需要获取索引”。。你…吗?您可能不需要索引,除非这是一些衍生的、非真实世界的场景/技术练习。给每个容器一个“data idx=”和它所需要的实际值,并使用
    $(this).closest(“div.y”).data(“idx”)
    我已经阅读了文档,但无法使它工作。我能找到的最接近的方法是将集合传递给
    .index()
    ,我已经尝试过了。但是不起作用(同样,他们的集合示例是vanilla js document.getElementById——这对我来说不起作用;尝试进行调整,但无法使其起作用)。如果不先浏览文档和搜索,我不会发帖,我不知道为什么会投反对票。就因为我说了“.index()”对我没有帮助“?@freedomn-m-这不是理论,我为什么要浪费时间?”我有一个水平列表,在鼠标悬停在具有
    y
    类的元素上时,我需要应用一个效果,如果它是第一个
    y
    类元素,或者如果它是最后一个,效果会有所不同。所以有三种效果:第一、最后和中间的任何效果。我在这里“添加”的唯一一件事是暗示我的
    y
    类元素不一定是一个接一个的,但在实际场景中它们总是连续的。在这里做的很简单,这样我就不会被太多的html所污染。我的语义类确实比
    x
    y
    my-con>多