-2

1 つの div を別の div に置き換え、選択されたときに 1 つのリンクを別のリンクに置き換える jquery スクリプトがあります。

$('.child2, a[href="#1"]').hide()

$('#replacepagelinks a').click(function(){
     $(this).hide().siblings().show()
     var w = this.hash.replace('#', '');
     $('#parent div.child'+w).show().siblings().hide()
})

これが私のhtmlです:

<div id="parent">
   <div class="child1">Child 1 Contents</div>
   <div class="child2">Child 2 Contents</div>
</div>
<div id="replacepagelinks">
     <a href="#1">Replace Child 1 with Child 2</a>
     <a href="#2">Replace Child 2 with Child 1</a>
</div>

実際の例については、この jsfiddle http://jsfiddle.net/KaqZ5/を確認してください。

これはうまく機能しますが、次のように最大 5 つの div で動作する必要があります。

  1. child1 で私はしたいだけ<a href="#1">Replace Child 1 with Child 2</a>

  2. child2 で私はしたいと思い<a href="#2">Replace Child 2 with Child 1</a>ます<a href="#3">Replace Child 2 with Child 3</a>

  3. child3で私はしたいと思い<a href="#3">Replace Child 3 with Child 2</a>ます<a href="#4">Replace Child 3 with Child 4</a>

  4. child4で私はしたいと思い<a href="#4">Replace Child 4 with Child 3</a>ます<a href="#5">Replace Child 4 with Child 5</a>.

  5. child5 で私はしたいだけ<a href="#5">Replace Child 5 with Child 4</a>

したがって、各ページには「次へ」リンクと「戻る」リンクがあり、最初のページには「次へ」リンクのみがあり、最後のページには「戻る」リンクのみがあり、2 つの子 div、3 つの子をサポートするためにこれが必要ですdiv と 4 つの子 div も同様です。

私のjqueryの経験はほとんどないので、誰かが私のjsfiddleを更新してくれたら、とても感謝しています。

4

2 に答える 2

1

http://jsfiddle.net/Palpatim/KaqZ5/7/で更新されたフィドルを参照してください。

改訂された HTML:

<div class="menu">
    <div class="menu_item">
        <div class="parent">
            <div class="child1">Child 1 Contents</div>
            <div class="child2">Child 2 Contents</div>
            <div class="child3">Child 3 Contents</div>
            <div class="child4">Child 4 Contents</div>
            <div class="child5">Child 5 Contents</div>
        </div>
        <div class="nav">
            <a class="prev" href="#">Back</a>
            <a class="next" href="#">Next</a>
        </div>
    </div>

    <div class="menu_item">
        <div class="parent">
            <div class="child1">Child 1 Contents</div>
            <div class="child2">Child 2 Contents</div>
            <div class="child3">Child 3 Contents</div>
        </div>
        <div class="nav">
            <a class="prev" href="#">Back</a>
            <a class="next" href="#">Next</a>
        </div>
    </div>
</div>

新しい CSS

/* Initially hide all menu items */
.parent div {
    display: none;
}

/* Show active item */
.parent div.active {
    display: block;
}

/* If a nav link is disabled, color it grey and set the cursor to an arrow */
a[disabled="disabled"] {
    color: #999;
    cursor: default;
}

<strong>改訂された JavaScript

// Set the initial menu state: Each 'prev' link is disabled and the first menu item 
// for each menu block is active
$('a.prev').attr('disabled', 'disabled');
$('.parent div:first-child').addClass('active');

$('a.next').click(function() {
    // Find the parent menu container 
    // - parent of (this) is div.nav
    //   - parent of div.nav is div.menu_item
    var menu = $(this).parent().parent();

    // Find the currently active menu item
    var active = $('.active', menu);

    // Find the next menu item in the list
    var nextItem = active.next();

    // If there is a next menu item, make it active
    if (nextItem.length) {
        // Make current item inactive
        active.removeClass('active');
        // Make new item active
        nextItem.addClass('active');

        // If there's no item after the new item,
        // disable navigation
        if (!nextItem.next().length) {
            $('a.next', menu).attr('disabled', 'disabled');
        }

        // If we just clicked 'next', we can enable the 'prev' button
        if ($('a.prev', menu).attr('disabled') == 'disabled') {
            $('a.prev', menu).removeAttr('disabled');
        }
    }
});

// Pretty much same as above, with the directions reversed
$('a.prev').click(function() {
    var menu = $(this).parent().parent();
    var active = $('.active', menu);
    var prevItem = active.prev();
    if (prevItem.length) {
        active.removeClass('active');
        prevItem.addClass('active');
        if (!prevItem.prev().length) {
            $('a.prev', menu).attr('disabled', 'disabled');
        }
        if ($('a.next', menu).attr('disabled') == 'disabled') {
            $('a.next', menu).removeAttr('disabled');
        }
    }
});

ここに私があなたのオリジナルから行ったいくつかの重要な変更があります:

  • .activeCSS を使用してメニュー項目を非表示にし、クラスを使用して再表示します。これにより、マークアップがきれいになり、スタイルを整えることができます
  • CSS を使用して、ナビゲーション リンクを完全に非表示にするのではなく、視覚的に無効にします。もちろん、単純に非表示にすることもできますが、それが本当にユーザー ナビゲーションである場合は、リンクをビューから完全に削除するのではなく、単にリンクが適用できないことをユーザーが確認できるようにする必要があります。
  • 視覚的に「次へ」の前に「戻る」を配置するようにナビゲーションを再構築しました。本当に削除したい場合は、無効なアンカーの CSS 定義を変更するだけです。
  • 代わりにクラスを持つようにリンクを変更しました

考慮すべきその他の最適化:

  • ナビゲーション コードの中身をスタンドアロン関数にリファクタリングします。前進する場合も後退する場合もほぼ同じなので、親のメニュー要素と目的の方向を渡すことで、関数をより抽象化することを検討してください。
  • http://www.xarg.org/2011/09/jquery-pagination-revised/のような jQuery ページネーション プラグインを使用してくださいあなたが自分でできるよりもよくテストされています。
  • ページに 2 つのナビゲーション ブロックがありますが、それぞれにid競合する属性があります。ids はページ上で一意である必要があります。代わりにそれらをクラスに変更しました。
  • メニューに項目が 1 つある場合、または項目がない場合を考えてみましょう。このコードは、これらの状況で窒息します

これを行うにはおそらく他にも多くの方法がありますが、これで始めることができます。さらに読むために、jQuery のドキュメントを熟読することをお勧めします。

于 2012-10-18T14:56:48.607 に答える
0

これが私が思いついたものです:

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
 <script type="text/javascript">
    function showPage(x){
        $("#parent div").hide();    //hide all pages        
        $('#parent div.child'+x).show();    //then show selected page           
    }
    function showLinks(x){              
        $('#replacepagelinks a').hide(); //hide all links               
        if(x>1)         
            $('#replacepagelinks a#'+(parseInt(x)-1)).show(); //if current page isn't the first one show back link

        $('#replacepagelinks a#'+(parseInt(x)+1)).show();  // show next link
    }
    $(document).ready(function(){                                   
        //$('#replacepagelinks span').on('click',function(e){       
        $(document).on('click', '#replacepagelinks a', function (e) {
            e.preventDefault();             
            var w = $(this).attr('id');
            showPage(w);
            showLinks(w);                       
        });     
    });
</script>
</head>
<body onload="showPage(1);showLinks(1);">
 <div id="parent">
  <div class="child1">Child 1 Contents</div>
  <div class="child2">Child 2 Contents</div>
  <div class="child3">Child 3 Contents</div>
  <div class="child4">Child 4 Contents</div>
</div>
<div id="replacepagelinks">
 <a href="#1" id="1">page 1</a>
 <a href="#2" id="2">page 2</a>
 <a href="#3" id="3">page 3</a>
 <a href="#4" id="4">page 4</a>  
</div>

</body>
</html>
于 2012-10-18T14:56:21.313 に答える