简体   繁体   English

隐藏并显示div的

[英]hiding and showing div's

I would like to be able to click on a link and for it to show the div section, is there an easy jQuery solution? 我希望能够单击一个链接并显示div部分,是否有简单的jQuery解决方案?

eg click on one will show div-one, and hide div-two & div-three, click two will show div-two and hide div-one and div-three 例如,单击一个将显示div-1,并隐藏div-2和div-3,单击两个将显示div-2,并隐藏div-one和div-3

<div class="div-one" >
one
</div>

<div class="div-two" style="display: none">
two
</div>

<div class="div-three" style="display: none">
three
</div>

<a href="#" class="one">one</a>
<a href="#" class="two">two</a>
<a href="#" class="three">three</a>

If your anchor only has one class then you can do: 如果您的锚只有一堂课,那么您可以:

$('a').click(function(e) {
    e.preventDefault();
    var cls = $(this).prop('class');
    $('.div-'+cls).show().siblings('div').hide();    
});

Fiddle Demo 小提琴演示

or give your anchor a data attribute: 或为锚定数据属性:

<a href="#" data-target="div-one" class="one">one</a>
<a href="#" data-target="div-two" class="two">two</a>
<a href="#" data-target="div-three" class="three">three</a>

then you can do: 那么您可以执行以下操作:

$('a').click(function(e) {
    e.preventDefault();
    var cls = $(this).attr('data-target');
    $('.'+cls).show().siblings('div').hide();    
});

Fiddle Demo 小提琴演示

try this 尝试这个

<a href="#" class="one" onclick="show_div(this)">one</a>
<a href="#" class="two" onclick="show_div(this)">two</a>
<a href="#" class="three" onclick="show_div(this)">three</a>
<script>
function show_div(el){
 $('.div-'+el.className).toggle(); // toggle could be replaced with show()/hide()
}
</script>

Check Live jsFiddle 检查Live jsFiddle

HTML 的HTML

<div class="buttons">
<a  id="showall">All</a>
<a  class="showSingle" target="1">Div 1</a>
<a  class="showSingle" target="2">Div 2</a>
<a  class="showSingle" target="3">Div 3</a>
<a  class="showSingle" target="4">Div 4</a>
</div>

<div id="div1" class="targetDiv">Menu1</div>
<div id="div2" class="targetDiv">Menu2</div>
<div id="div3" class="targetDiv">Menu3</div>
<div id="div4" class="targetDiv">Menu4</div>

JQuery jQuery查询

 jQuery(function(){
     jQuery('#showall').click(function(){
           jQuery('.targetDiv').show();
    });
    jQuery('.showSingle').click(function(){
          jQuery('.targetDiv').hide();
          jQuery('#div'+$(this).attr('target')).show();
    });
});

You can achieve your goal easily using jQuery. 您可以使用jQuery轻松实现目标。 Please find the way to do this below - 请在下面找到执行此操作的方法-

jQuery("a").click(function() {
   jQuery("div").hide();
    if(this.className == 'one')
        jQuery(".div-one").show();
    if(this.className == 'two')
        jQuery(".div-two").show();
    if(this.className == 'three')
        jQuery(".div-three").show();
});

Try Demo - http://jsfiddle.net/yrM3H/1356/ 尝试演示-http://jsfiddle.net/yrM3H/1356/

You can use show() and hide() functions in jquery to achive this. 您可以在jquery中使用show()和hide()函数来实现此目的。

$(".div_name").hide(); $(“。div_name”)。hide(); // to hide div $(".div_name").show(); //隐藏div $(“。div_name”)。show(); // to show div //显示div

You can use css class for this: 您可以为此使用css类:

I have used same class for anchor and corresponding div. 我对锚和相应的div使用了相同的类。 I have used 2 css classes(hide and show). 我已经使用了2个CSS类(隐藏和显示)。

when some click in the anchor tag: 当点击锚标签时:
-- i have removed show class of previous div and added hide class. -我删除了上一个div的show类并添加了hide类。
-- i have added show class and removed hide of corresponding div by class name. -我添加了show类,并通过类名称删除了对应div的隐藏。

HTML: HTML:

<div class="div-one show" >
one
</div>

 <div class="div-two hide" style="display: none" >
two
</div>

<div class="div-three hide" style="display: none">
three
</div>

<a href="#" class="div-one">one</a>
<a href="#" class="div-two">two</a>
<a href="#" class="div-three">three</a>

JQUERY: JQUERY:

$(document).ready(function(){
    $("a[class^='div-']").click(function(e){
        e.preventDefault();
        strClass= $(this).attr('class');
        $(".show").removeClass("show").addClass("hide");
        $("div."+strClass).removeClass("hide").addClass("show");

    })
});

jsFiddle jsFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM