admin管理员组

文章数量:1317572

I want to show 2 links - Show and Hide. When I click on Show, Show should be hidden and Hide should be visible and when I click on Hide, Hide should be hidden and Show should be visible. How can I achieve this in html?

I want to show 2 links - Show and Hide. When I click on Show, Show should be hidden and Hide should be visible and when I click on Hide, Hide should be hidden and Show should be visible. How can I achieve this in html?

Share Improve this question edited Dec 19, 2011 at 10:34 kubetz 8,5661 gold badge24 silver badges27 bronze badges asked Dec 7, 2011 at 8:23 JaveDeveloperJaveDeveloper 1332 gold badges7 silver badges18 bronze badges 3
  • 6 You can achieve it alot of ways. What have you done? – Dan Lugg Commented Dec 7, 2011 at 8:24
  • You can do that by display property in style by giving "none" and "block". This is one of the easiest way and I hope you can do by yourself instead of exact code from us. – Selvakumar Ponnusamy Commented Dec 7, 2011 at 8:33
  • @NsDeep Next time you change an accepted answer to the jQuery one even when you haven't specified that you want to use that library, be so kind and add a jQuery tag to the Q. It is a standard here to add jQuery tag if you are interested in using jQuery library. – kubetz Commented Dec 19, 2011 at 10:40
Add a ment  | 

2 Answers 2

Reset to default 2

with jquery in the head

<script src='http://ajax.googleapis./ajax/libs/jquery/1.6.4/jquery.min.js'></script>

and an id for each button

<input type='button' id='show' value='Show' />
<input type='button' id='hide' value='Hide' />

you can do it something like this untested code...

<script type='text/javascript'>
    $(function(){
        $('#show').click(function(){
            $('#hide').hide()
        })
        $('#hide').click(function(){
            $('#show').hide()
        })
    })
</script>

Do you really only want to hide "hide" when "show" is clicked and vice versa? Because that is what you asked for, but it doesn't really sound as a "standard" (toggle) behaviour.

But here it is:

See this snippet.

HTML:

<a href="#" id="show">SHOW</a>
<a href="#" id="hide">HIDE</a>

JS:

var showElem = document.getElementById("show");
var hideElem = document.getElementById("hide");

showElem.onclick = function() {
   hideElem.style.display = 'none';
}

hideElem .onclick = function() {
   showElem.style.display = 'none';
}

Instead of .style.display = 'none'; you can use .style.visibility = 'hidden' which will still hide the link, but there will be empty space instead of it and it won't really pletely dissapear (see this snippet).

Update: Based on discussion in the ments section new simple example was created (see this) that have more "standard" behaviour. In case the page is expected to be heavy on effects or other javascript functionality (like Ajax) I would remend to use 3rd party library like jQuery to simplify the implementation - see answer by Billy Moon.

本文标签: javascriptHideShow Links in htmlStack Overflow