最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Show Only One Element with JQuery - Stack Overflow

programmeradmin3浏览0评论

I need to show only one element at a time when a link is clicked on. Right now I'm cheating by hiding everything again and then toggling the element clicked on. This works, unless i want EVERYTHING to disappear again. Short of adding a "Hide All" button/link what can i do? I would like to be able to click on the link again, and hide it's content.

EDIT: Pseudo's code would have worked, but the html here mistakenly led you to believe that all the links were in one div. instead of tracking down where they all were, it is easier to call them by their ID.

Here's what I have so far:

$(document).ready(function(){
    //hides everything
    $("#infocontent *").hide();

    //now we show them by which they click on
    $("#linkjoedhit").click(function(event){
      $("#infocontent *").hide();
      $("#infojoedhit").toggle();
      return false;
    });

    $("#linkgarykhit").click(function(event){
      $("#infocontent *").hide();
      $("#infogarykhit").toggle();
      return false;
    });

});

and the html looks like:

<div id="theircrappycode">
    <a id="linkjoedhit" href="">Joe D</a><br/>
    <a id="linkgarykhit" href="">Gary K</a>
</div>

<div id="infocontent">
    <p id="infojoedhit">Information about Joe D Hitting.</p>
    <p id="infogarykhit">Information about Gary K Hitting.</p>
</div

there are about 20 links like this. Because I am not coding the actual html, I have no control over the actual layout, which is horrendous. Suffice to say, this is the only way to organize the links/info.

I need to show only one element at a time when a link is clicked on. Right now I'm cheating by hiding everything again and then toggling the element clicked on. This works, unless i want EVERYTHING to disappear again. Short of adding a "Hide All" button/link what can i do? I would like to be able to click on the link again, and hide it's content.

EDIT: Pseudo's code would have worked, but the html here mistakenly led you to believe that all the links were in one div. instead of tracking down where they all were, it is easier to call them by their ID.

Here's what I have so far:

$(document).ready(function(){
    //hides everything
    $("#infocontent *").hide();

    //now we show them by which they click on
    $("#linkjoedhit").click(function(event){
      $("#infocontent *").hide();
      $("#infojoedhit").toggle();
      return false;
    });

    $("#linkgarykhit").click(function(event){
      $("#infocontent *").hide();
      $("#infogarykhit").toggle();
      return false;
    });

});

and the html looks like:

<div id="theircrappycode">
    <a id="linkjoedhit" href="">Joe D</a><br/>
    <a id="linkgarykhit" href="">Gary K</a>
</div>

<div id="infocontent">
    <p id="infojoedhit">Information about Joe D Hitting.</p>
    <p id="infogarykhit">Information about Gary K Hitting.</p>
</div

there are about 20 links like this. Because I am not coding the actual html, I have no control over the actual layout, which is horrendous. Suffice to say, this is the only way to organize the links/info.

Share Improve this question edited Feb 21, 2016 at 15:07 rrk 15.9k4 gold badges30 silver badges47 bronze badges asked Oct 3, 2008 at 21:07 helloandrehelloandre 10.7k9 gold badges49 silver badges64 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2
$("#linkgarykhit").click(function(){
   if($("#infogarykhit").css('display') != 'none'){
      $("#infogarykhit").hide();
   }else{
      $("#infocontent *").hide();
      $("#infogarykhit").show();
   }
   return false;
});

We could also DRY this up a bit:

function toggleInfoContent(id){
   if($('#' + id).css('display') != 'none'){
      $('#' + id).hide();
   }else{
      $("#infocontent *").hide();
      $('#' + id).show();
   }
}

$("#linkgarykhit").click(function(){
    toggleInfoContent('infogarykhit');
    return false;
});

$("#linkbobkhit").click(function(){
    toggleInfoContent('infobobkhit');
    return false;
});

If your markup "naming scheme" is accurate, you can avoid a lot of repetitious code by using a RegEx for your selector, and judicious use of jQuery's "not".

You can attach a click event one time to a jQuery collection that should do what you want so you don't need to add any JavaScript as you add more Jim's or John's, as so:

$(document).ready( function () {
  $("#infocontent *").hide();

  $("div#theircrappycode > a").click(function(event){
    var toggleId = "#" + this.id.replace(/^link/,"info");
    $("#infocontent *").not(toggleId).hide();
    $(toggleId).toggle();
    return false;
  });
});

Here is a slightly different approach there are some similarities to Pseudo Masochist's code.

$(document).ready(function(){
  $("#infocontent *").hide();
  $("#theircrappycode > a").click(statlink.togvis);
});
var statlink = {
  visId: "",
  togvis: function(){
    $("#" + statlink.visId).toggle();
    statlink.visId = $(this).attr("id").replace(/link/, "info");
    $("#" + statlink.visId).toggle();
  }
};

Hope you find this useful also.

I just started with jQuery, so I don't know if this is dumb or not.

function DoToggleMagic(strParagraphID) {
  strDisplayed = $(strParagraphID).css("display");
  $("#infocontent *").hide();
  if (strDisplayed == "none") 
    $(strParagraphID).toggle();
}
$(document).ready(function(){
  //hides everything
  $("#infocontent *").hide();

  //now we show them by which they click on
  $("#linkjoedhit").click(function(event){
    DoToggleMagic("#infojoedhit");
    return false;
  });

  $("#linkgarykhit").click(function(event){
    DoToggleMagic("#infogarykhit");
    return false;
  });        
});
发布评论

评论列表(0)

  1. 暂无评论