1

jQueryを使用して同じdiv領域を表示/非表示にするにはどうすればよいですか...基本的にコンテンツを交換しますか?

これが私がこれまでに持っているものであり、以前のコンテンツを非表示にして新しいコンテンツに置き換える方法がわかりません。

       <script type="text/javascript">
    $(document).ready(function(){

    $('.content').hide();

    $('a').click(function(){
    $('.content').show('slow');
    });

    //then hide previous content

    });

    </script>

<a href="#" id="a-link">A</a>
<a href="#" id="b-link">B</a>

  <div class="start-content" id="a">
  content here
  </div>

  <div class="content" id="b">
  content here
  </div>
4

2 に答える 2

2

内容をdivでラップすると、次のように少し簡単になります。

<div id="wrap">
  <div class="start-content" id="a">
  content here
  </div>

  <div class="content" id="b">
  content here
  </div>
</div>

そしてこれはjQueryで:

$('a').click(function(){
  $("#wrap > div").hide(); //hide previous
  $('.content').show('slow'); //show what's clicked on
});

ただし、IDには規則があるので、それを使用して、リンクをクラスに与えるか、次のようにそれらをラップすることもできます。

<div id="links">
  <a href="#" id="a-link">A</a>
  <a href="#" id="b-link">B</a>
</div>
<div id="wrap">
  <div class="start-content" id="a">
  content here
  </div>    
  <div class="content" id="b">
  content here
  </div>
</div>

次に、次のようにすべてのリンクに単一のイベントハンドラーを使用できます。

$('#wap > div:not(.start-content)').hide(); //Initial hide
$("#links a").click(function() {
  $("#wrap > div").hide(); //hide previous
  var id = $(this).attr("id").replace('-link', ''); //matching id
  $('#' + id).show('slow'); //show what's clicked on
});
于 2010-03-30T15:19:34.070 に答える
0

コンテンツdivを表示したままにします。代わりに、HTMLコンテンツを $('.content').html(strvalue);

于 2010-03-30T15:17:36.893 に答える