简体   繁体   English

jQuery .css display:none

[英]jQuery .css display:none

I'm trying to do the following: 我正在尝试执行以下操作:

  • I have 6 div. 我有6个div。 Inside each div is another div with display:none. 在每个div里面是另一个带显示的div:none。

I want to do in jQuery when i click on each div all 6 div change to display:none and the div inside the div clicked to show in the screen. 我想在jQuery中点击每个div所有6 div更改为显示:none并且div内的div点击显示在屏幕上。

Check this fiddle http://jsfiddle.net/Weinz/xaMfk/2/ 检查这个小提琴http://jsfiddle.net/Weinz/xaMfk/2/

The only thing that happen it's all goes display:none 唯一发生的事情就是显示:无

What can i do? 我能做什么? I try also with show and hide and doesn't work 我也尝试使用show和hide并且不起作用

Thank you in advance 先感谢您

Glad you got your answer above. 很高兴你得到了上面的回答。 @Zoltan's approach looks like it could work for you. @ Zoltan的方法看起来可能适合你。 I would also consider maybe adding classes to the divs as it would reduce the jquery and css. 我也会考虑在div中添加类,因为它会减少jquery和css。

<div class="outerDiv" id="box1">Box1</div>
<div id="cont1" class="innerDiv" style="display:none"><p>Some text and img</p></div>
<div class="outerDiv" id="box2">Box2</div>
<div id="cont2" class="innerDiv" style="display:none"><p>Some text and img</p></div>
<div class="outerDiv" id="box3">Box3</div>
<div id="cont3" class="innerDiv" style="display:none"><p>Some text and img</p></div>    
<div class="outerDiv" id="box4">Box4</div>
<div id="cont4" class="innerDiv" style="display:none"><p>Some text and img</p></div>
<div class="outerDiv" id="box5">Box5</div>
<div id="cont5" class="innerDiv" style="display:none"><p>Some text and img</p></div>



$(function() {
    $(".outerDiv").click(function() {
        $(".outerDiv").hide();
        $(".innerDiv").hide();
        $(this).next("div").show();
    });
});​
.outerDiv{border: 1px solid;}​ 
div,div p {display:inline;}
<div id="box1" class="outer">Box1<div id="cont1" class="inner" style="display:none"><p>Some text and img</p></div></div>
<div id="box2" class="outer">Box2<div id="cont2" class="inner" style="display:none"><p>Some text and img</p></div></div>
<div id="box3" class="outer">Box3<div id="cont3" class="inner" style="display:none"><p>Some text and img</p></div></div>

$(function(){
    $(".outer").click(function(){  
        var id= this.id;
        $("#"+id+" .inner").show(); 
//user toggle instead of show if you need toggle

   });

});

This code will hep you, if you are intened to show each hidden div when a user clicks parent div. 如果您打算在用户单击父div时显示每个隐藏的div,则此代码将为您提供帮助。 If you hide parent div, then the inner content will also be hidden. 如果隐藏父div,则内部内容也将被隐藏。

Try this demo 试试这个演示

As I understand your question this is what you need. 据我了解你的问题,这就是你所需要的。

JS JS

$(function(){
   $("#wrapper").on('click', 'div.maindiv',function(){
       $(this).find('div.innerdiv').show();
       $(this).siblings('div.maindiv').hide();

   });
});

This will show whatever the inner div content of the div you clicked and will hide the other divs on the same element level. 这将显示您单击的div的内部div内容,并将隐藏同一元素级别上的其他div。 Don't forget to try the demo out and give the feedback.( link is given at the beginning of the answer ) 不要忘记尝试演示并提供反馈。( 链接在答案的开头给出

Hope this will solve your problem. 希望这能解决你的问题。 If you have any questions please don't hesitate to ask. 如果您有任何疑问,请随时提出。 Thanks. 谢谢。

try something like this, jsfiddle : 试试这样的东西, jsfiddle

// HTML:
<div id="box1">Box1<div id="cont1" style="display:none"><p>Some text and img</p></div></div>
<div id="box2">Box2<div id="cont2" style="display:none"><p>Some text and img</p></div></div>
<div id="box3">Box3<div id="cont3" style="display:none"><p>Some text and img</p></div></div>
<div id="box4">Box4<div id="cont4" style="display:none"><p>Some text and img</p></div></div>
<div id="box5">Box5<div id="cont5" style="display:none"><p>Some text and img</p></div></div>

// CSS:
div {
    display:inline;
}
#box1 {border: 1px solid;}
#box2 {border: 1px solid;}
#box3 {border: 1px solid;}
#box4 {border: 1px solid;}
#box5 {border: 1px solid;}

// JS:
$(function(){
   $("#box1").click(function(){
       $(this).css("display","");
       //$("#box1").css("display","none");
       $("#box2").css("display","none");
       $("#box3").css("display","none");
       $("#box4").css("display","none");
       $("#box5").css("display","none");
   });
});

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

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