代码之家  ›  专栏  ›  技术社区  ›  spicedham

查找非同级下一个元素

  •  0
  • spicedham  · 技术社区  · 7 年前

    之后 当我单击下一个链接时,标记为“活动”的链接。我下面的示例html结构对我有限的jQuery知识来说意味着。next不起作用,因为标记元素不是同级元素。最终结果应该是,单击下一个链接,然后单击“pizza”一词周围的链接。

    <div class="wrapper">
    <p>some <a href="#" class="tag">text</a>here and more <a href="#" class="tag">text</a></p>
    <p>some <a href="#" class="tag active">text</a>here</p>
    <p>some <a href="#" class="tag">pizza</a>here and more <a href="#" class="tag">text</a></p>
    <p>some <a href="#" class="tag">text</a>here and some more <a href="#" class="tag">text</a></p>
    </div>
    
    <div class="nav">
    <a href="#" class="back">Back</a>
    <a href="#" class="next">Next</a>
    </div>
    

    类似的内容只在一个段落内有效

    $(".next").click(function() {
        $(".active").next().click();
    });
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Roamer-1888    7 年前

    这一切都是为了给 .back , .next .tag

    • “findPrev”事件处理程序,用于查找集合中的前一个标记,
    • “findNext”事件处理程序,用于查找集合中的下一个标记。
    $(document).ready(function() {
        $(".nav .back").on('click', function(e) {
            e.preventDefault();
            if(this.href) { $(".wrapper .active").triggerHandler('findPrev').click(); }
        });
        $(".nav .next").on('click', function(e) {
            e.preventDefault();
            if(this.href) { $(".wrapper .active").triggerHandler('findNext').click(); }
        });
    
        $(".tag").on('findPrev', function() { // <<< custom event handler
            var $tags = $(this).closest('.wrapper').find('.tag');
            var index = $tags.index(this);
            return (index > 0) ? $tags.eq(index - 1) : $();
        }).on('findNext', function() { // <<< custom event handler
            var $tags = $(this).closest('.wrapper').find('.tag');
            var index = $tags.index(this);
            return (index < $tags.length) ? $tags.eq(index + 1) : $();
        }).on('click', function(e) {
            e.preventDefault();
            $(".wrapper .tag").filter(".active").removeClass('active').end().filter(this).addClass('active'); // move the 'active' highlight
            // desired click action here
        }).filter(".active").trigger('click');
    });
    

    Demo

    一旦你想好了,作为奖励,添加几行额外的代码来启用/禁用 Back Next 按钮以响应单击标签。这可以包括更多的自定义事件处理程序:

    • 用于后面和后面元素的“启用”事件处理程序,
    • 用于后面和后面元素的“禁用”事件处理程序。
    $(document).ready(function() {
        $(".nav .back").on('click', function(e) {
            e.preventDefault();
            if(this.href) { $(".wrapper .active").triggerHandler('findPrev').click(); } // find previous tag and 'click' it.
        });
        $(".nav .next").on('click', function(e) {
            e.preventDefault();
            if(this.href) { $(".wrapper .active").triggerHandler('findNext').click(); } // find next tag and 'click' it.
        });
        $(".nav .back, .nav .next").on('enable', function() { // <<< custom event handler
            $(this).attr('href', '#'); // enable
        }).on('disable', function() { // <<< custom event handler
            $(this).removeAttr('href'); // disable
        });
    
        $(".tag").on('findPrev', function() { // <<< custom event handler
            var $tags = $(this).closest('.wrapper').find('.tag');
            var index = $tags.index(this);
            return (index > 0) ? $tags.eq(index - 1) : $();
        }).on('findNext', function() { // <<< custom event handler
            var $tags = $(this).closest('.wrapper').find('.tag');
            var index = $tags.index(this);
            return (index < $tags.length) ? $tags.eq(index + 1) : $();
        }).on('click', function(e) {
            e.preventDefault();
            $(".wrapper .tag").filter(".active").removeClass('active').end().filter(this).addClass('active'); // move the 'active' highlight
            $(".nav .back").trigger($(this).triggerHandler('findPrev').length ? 'enable' : 'disable'); // manage the back button
            $(".nav .next").trigger($(this).triggerHandler('findNext').length ? 'enable' : 'disable'); // manage the next button
            // desired click action here
        }).filter(".active").trigger('click'); // trigger 'click' to initialize everything
    });
    

    Demo

    • .trigger() .triggerHandler() 可能令人困惑。区别在于返回的内容。 .triggerHandler() 返回处理程序返回的任何内容。
    • <button> 元素,而不是超链接。可以固有地禁用/启用适当的按钮,而不必对 href 属性
    • 定制事件也可以用jQuery插件来表达,这是可行的,但可以说是简单功能的最佳选择。
        2
  •  0
  •   Louys Patrice Bessette    7 年前

    编辑

    如果要循环所有标记,可以给它们一个自定义属性,以便于查找。

    $(document).ready(function(){
    
      // Get all tags.
      var tagCollection = $(".tag");
    
      // Give them an "index"
      tagCollection.each(function(index){
        //console.log( index );
        $(this).attr("data-index",index);
      });
    
      // Click handler
      $(".next").click(function() {
        
        // Get the index of the active tag +1 (for the next).
        var dataIndex = parseInt( $(".active").attr("data-index") )+1;
        //console.log(dataIndex);
    
        // If last index, back to the very first.
        if(dataIndex>tagCollection.length-1){
          dataIndex = 0;
        }
    
        // Here, we remove the active class on the current tag
        // And find the next one to add the active class on it.
        // For that demo, I turned it to red.
        // You may click it!
        $(document).find(".active")                       // Find the active one.
                   .removeClass("active")                 // Remove the class
                   .closest(".wrapper")                   // climb up to the wrapper
                   .find("[data-index='"+dataIndex+"']")  // to find the next tag
                   .addClass("active")                    // Give it the class
                   .click();                              // And click it!
    
      });
      
      // Tag click handler
      $(".tag").click(function(){
        console.log( $(this).text() );
      });
      
    });
    .active{
      color:red;
      font-weight:bold;
      text-decoration:none;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div class="wrapper">
      <p>some <a href="#" class="tag">text</a>here and more <a href="#" class="tag">text</a></p>
      <p>some <a href="#" class="tag active">text</a>here</p>
      <p>some <a href="#" class="tag">pizza</a>here and more <a href="#" class="tag">text</a></p>
      <p>some <a href="#" class="tag">text</a>here and some more <a href="#" class="tag">text</a></p>
    </div>
    
    <div class="nav">
    <a href="#" class="back">Back</a>
    <a href="#" class="next">Next</a>
    </div>

    整页运行此代码段;)
    我相信你将能够在“Back”链接上应用相同的逻辑。