【问题标题】:Find non-sibling next element查找非同级元素
【发布时间】:2018-02-12 04:49:06
【问题描述】:

尝试点击下一个“标签”链接当我点击“下一个”链接时标记为“活动”的那个链接。下面我的示例 html 结构意味着我有限的 jQuery 知识 .next 不会工作,因为标签元素不是兄弟姐妹。最终结果应该是点击 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();
});

【问题讨论】:

    标签: jquery select next siblings


    【解决方案1】:

    编辑

    如果你想循环所有标签,你可以给它们一个自定义属性来简化它们的查找。

    查看代码中的 cmets。

    $(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>

    在整页中运行这个 sn-p ;)
    我相信您将能够在“返回”链接上应用相同的逻辑。

    【讨论】:

      【解决方案2】:

      这就是为.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

      一旦您考虑到这一点,作为奖励,添加一些额外的行来启用/禁用BackNext 按钮以响应单击标签是相对简单的。这可以包括更多的自定义事件处理程序:

      • Back 和 Next 元素的“启用”事件处理程序,
      • 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() 可能会造成混淆。不同之处在于返回的内容。 .trigger() 总是返回 jQuery(用于链接),而 .triggerHandler() 返回处理程序返回的任何内容。
      • 使用 HTML &lt;button&gt; Back 和 Next 元素代替超链接可以稍微简化事情。可以固有地禁用/启用适当的按钮,而不会与href 属性混淆。
      • 自定义事件也可以表述为 jQuery 插件,这是可行的,但对于简单的功能来说可能是多余的。

      【讨论】:

      • 这也对我有用!开始学习看看不同的事情可以有多么不同,并且仍然做同样的事情。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多