2017-09-03 41 views
0

次の "タグ"リンクをクリックしようとしました。の後に、次のリンクをクリックすると "アクティブ"とマークされています。私のサンプルhtml構造は、タグ要素が兄弟ではないため、次に動作しないjQueryの知識に限定されています。最終結果は、次のリンクをクリックすると、「ピザ」という言葉の周りのリンクをクリックする必要があります。兄弟でない次の要素を見つける

<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'イベントハンドラ。
あなたはそれがで BackNextボタンを有効/無効にするには、いくつかの余分な行を追加することは比較的些細だボーナスとして、そのラウンドあなたの心を持ったら
$(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

タグをクリックすると応答します。戻ると、次の要素のイベントハンドラ、

  • 戻ると次の要素のための「無効」イベントハンドラを「有効にする」

    • :これはカップルより多くのカスタムイベントハンドラを含めることができます。
    $(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 <button>の要素で戻ると次の要素がわずかに簡略化されます。適切なボタンは、本質的には無効な/有効にすることができます.属性を迷惑にしないでください。
    • カスタムイベントはjQueryプラグインとして表現することもできますが、これは実行可能ですが間違いなく簡単な機能のためにはトップにすぎません。
  • +1

    これも私のために働いた!学ぶことを始めてどのように異なることができ、同じことをやっているのか見てみましょう。 – spicedham

    0

    EDIT

    ような何かあなたがすべてのタグのサイクルにしたい場合は、あなたがそれらに彼らの発見を容易にするカスタム属性を与えることができます。

    コードのコメントを参照してください。

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

    ランページ全体でこのスニペット;)
    私は、あなたが「戻る」リンク上で同じロジックを適用することができます確信しています。

    関連する問題