JQuery div显示,崩溃

时间:2014-02-24 09:55:40

标签: javascript jquery html

我不是jquery专家,但我设法让这个脚本在我的网站上运行:

<script>
$(document).ready(function() {
$('#open_#div_hidden_1').click(function() {
if ($('#div_hidden_1').is(':hidden')) {
        $('#div_hidden_1').show(500);
        $('#div_hidden_1').hide(500);


} else {
        $('#div_hidden_1').hide(500);
}
});
});

</script>

基本上它显示并折叠一个div(由id区分),我在我的wbesite上有很多以这种方式显示的div(它是一个内联代码,对于每个div单独的代码)我想用它来做的是当我打开另一个div时,关闭所有其他div(例如来自同一个类)。可以请某人帮我修改这段代码,以便它会折叠同一类的所有其他div吗?

2 个答案:

答案 0 :(得分:0)

如果你有多个DIV和类,如下所示,

<div class="divClass">A</div>
<div class="divClass">B</div>
<div class="divClass">C</div>

然后,你需要使用like,

$(".divClass").click(function(){
    $(".divClass").hide(500); //hiding all the element with divClass
    $(this).show(500); // showing up the clicked element.
});

答案 1 :(得分:0)

<强> This might be able to you
Reference
只是代码的一部分

$(".header").click(function () {
    $(".header").not(this).text('Expand').next().slideUp();
    $header = $(this);
    //getting the next element
    $content = $header.next();
    //open up the content needed - toggle the slide- if visible, slide up, if not slidedown.
    $content.slideToggle(500, function () {
        //execute this after slideToggle is done
        //change text of header based on visibility of content div
        $header.text(function () {
            //change text based on condition
            return $content.is(":visible") ? "Collapse" : "Expand";
        });
    });

});