2017-09-03 40 views
0

尝试点击下一个“标签”链接之后当我点击下一个链接时标记为“活动”的那个链接。我下面的示例html结构意味着我有限的jQuery知识。因为标签元素不是兄弟姐妹,.next不会起作用。最终的结果应该是点击下一步链接,然后点击围绕单词“披萨”的链接。找到非同胞的下一个元素

<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(); 
}); 

回答

0

这是关于给予.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按钮响应点击标签。这可以包括一对夫妇更多的自定义事件处理程序:

  • 了一步和下一步元素的“启用”的事件处理程序,
  • 一个“禁用”事件上一步和下一步的元素处理。
$(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()返回处理程序返回的任何内容。
  • 对于Back和Next代替超链接,事情会略微简化为HTML <button>元素。正确的按钮可以固有地禁用/启用,而不会与href属性混淆。
  • 自定义事件也可以表述为jQuery插件,这对于简单的功能来说是可行的,但可以说是顶级的。
+1

这也适用于我!开始学习,看看不同的事情会发生多大的变化,并且仍然会做同样的事情。 – spicedham

0

编辑

如果你想要周期的所有标签中,你可以给他们一个自定义属性,以减轻他们的发现。

请参阅代码中的注释。

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

运行这个片段在整页)
我敢肯定,你将能够运用上的“返回”链接相同的逻辑。

相关问题