скрыть и показать div нажмите на

0

я хочу скрыть идентификатор, когда я нажимаю на href, поэтому его один и тот же идентификатор показывает, и другой идентификатор автоматически закрывается

пример:

<div id="fit" class="heading">FIT</div>
<a href="#er">first</a>
<div id="er" style="display:none;">aksdjfhaskdj hskj hskjfh sd fghjgfdjf gdsjfdg jdfgjdf gjgdfjgfdjf gasdkjfasjfghsdj </div>
<a href="#erp">erp</a>
<div id="erp" style="display:none;">erp </div>
<div id="style" class="heading">style</div>

и скрипт:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function(e) {
    $("a").click(function(e) {
        var ab = $(this).attr("href");
        //alert(ab);
        //$("div").hide();
        $(ab).show();

    });
});
</script>

5 ответов

2

в классе использования html для связанного с привязкой div

<div id="fit" class="heading">FIT</div>
<a href="#er">first</a>
<div id="er" style="display:none;" class="anchorrel">aksdjfhaskdj hskj hskjfh sd fghjgfdjf gdsjfdg jdfgjdf gjgdfjgfdjf gasdkjfasjfghsdj </div>
<a href="#erp">erp</a>
<div id="erp" style="display:none;" class="anchorrel">erp </div>
<div id="style" class="heading">style</div>




<script>
    $(document).ready(function(e) {
        $("a").click(function(e) {
        e.preventDefault();
            var ab = $(this).attr("href");
            //alert(ab);
            $(".anchorrel").hide();// all div related to .anchorrel hide
            $(ab).show();

        });
    });
    </script>

см. демо

1

Вы можете сделать это:

$(document).ready(function (e) {

    // Cache the anchor tag here
    var $link = $('a');

    // Click event handler for the link
    $link.click(function (e) {

        // prevent the default action of the anchor
        e.preventDefault();

        var id = this.href;

        // Hide all the visible divs next to all the anchors
        $link.next('div:visible').hide();

        // Show the current div with id
        $(id).show();
    });
});
0

Если вы не закроете все остальные div и отобразите только div с идентификатором, который соответствует href тега, нажмите, а используйте следующее:

$("a").click(function(e) {
    var ab = $(this).attr("href");
    $('div').hide();
    $(ab).show();
});

Я не знаю, почему вы прокомментируете это в первую очередь, может быть, я не понимаю, чего вы не достигнете.

Вот скрипка: http://jsfiddle.net/25RZR/

  • 0
    он будет скрывать все div в контексте, не похоже на намерение OP
  • 0
    «и другие идентификаторы закроются автоматически» - это не очень легко понять. :)
0

Ну, вы можете сделать так:

$(document).ready(function(e) {
  $("a").click(function (e) {
      e.preventDefault; // <------------stop the default behavior
      var ab = $(this).attr('href'); // <------better to use in terms of performance
      $(ab).show().siblings('div:not(.heading)').hide();
  });
});

Демонстрация в действии

0

скрипка

JavaScript

$(document).ready(function(e) {
    $("a").click(function(e) {
        var ab = $(this).attr("href");  
        $('.content').hide();
        $(ab).show();

    });
});

HTML

<div id="fit" class="heading">FIT</div>
<a href="#er">first</a>
<div id="er" class="content hide">aksdjfhaskdj hskj hskjfh sd fghjgfdjf gdsjfdg jdfgjdf gjgdfjgfdjf gasdkjfasjfghsdj </div>
<a href="#erp">erp</a>
<div id="erp" class="content hide">erp </div>
<div id="style" class="heading">style</div>

Ещё вопросы

Сообщество Overcoder
Наверх
Меню