在IE6中,JQuery切换不能正确折叠

时间:2011-09-27 05:03:11

标签: javascript jquery html toggle collapse

我有3个div使用JQuery的切换功能来折叠div:

enter image description here

在Firefox中崩溃很好,但在IE6(目标浏览器)中,会发生这种情况:

enter image description here

如果我调整IE窗口的大小,div会恢复正常,就像在Firefox中一样。

我试图将代码简化为最简单的形式:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>BIIS Portal</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="cache" />
<link rel="stylesheet" type="text/css" media="all" href="../assets/stylesheets/core-css.css" />
<script type="text/javascript" src="../assets/js/core-js.js"></script>
<!-- This script doesn't seem to work when put it in its own .js file... why? -->
<script type="text/javascript">
$(document).ready(function(){
    //Hide (collapse) the toggle containers on load
    $(".toggle-container").hide(); 

    //Show all containers with class toggle-opened
    //Remove the opened class
    //Find the trigger for the now opened container and apply the active class
    $(".toggle-opened").show().removeClass("toggle-opened").prev(".toggle-trigger").addClass("active");

    //Switch the Open and Close state per click then slide up/down (depending on open/close state)
    $(".toggle-trigger").click(function(){
        $(this).toggleClass("active").next().toggle();
        return false; //Prevent the browser jump to the link anchor
    });
});
</script>
</head>
<body>
    <div id="wrapper">
        <div id="header">
        </div>
        <div id="body">
            <div>
                <div class="portlet">
                    <div class="portlet-header">
                        <div class="portlet-title">
                        <h2>BI - External data Control</h2>
                        </div>
                    </div>
                    <div class="portlet-body">
                        <div>
                            <h3 class="toggle-trigger">External Data Configuration</h3>
                            <div class="toggle-container toggle-opened">
                                blah
                            </div>
                            <h3 class="toggle-trigger">Current Notifications</h3>
                            <div class="toggle-container toggle-opened">
                                blah
                            </div>
                            <h3 class="toggle-trigger">General Information</h3>
                            <div class="toggle-container toggle-opened">
                                blah
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

芯css.css:

@import "base.css";
@import "framework.css";
@import "elastic.css";
@import "superfish.css";

@import "/application/css/jquery.autocomplete.css";
@import "/application/css/hspi-content-nav.css";
@import "/application/css/jquery-ui/jquery-ui-1.8.12.custom.css";

core-js.js只是几个缩小的JQuery库,即:

  • jQuery JavaScript Library v1.5.2
  • jQuery Cycle Plugin(带转换定义)版本:2.86(05-APR-2010)
  • jQuery Cycle Plugin Transition Definitions版本:2.72
  • jQuery UI 1.8.12
  • jQuery UI Widget 1.8.12
  • jQuery UI Mouse 1.8.12

我不太确定发生了什么,因为我主要复制了现有的代码。我需要让它在IE中正常工作,所以任何建议都值得赞赏。

3 个答案:

答案 0 :(得分:0)

使用.toggle-container和/或.toggle-closed为ie6添加条件注释(不确定是否使用该类)display:none

答案 1 :(得分:0)

一个常见的问题是事情根本不应该重新绘制。如果调整窗口大小或缩放页面工作,这是一个重绘错误,据我所知,除了试图找到一些其他有效的编码方式之外,你无法在代码中做任何事情。

答案 2 :(得分:0)

@米奇。我认为你的主要问题是你的CSS风格。你不提供CSS代码,但下一个例子在IE6-8和FF7的测试中工作(除了IE6-7不支持CSS content样式)http://jsfiddle.net/2YyXC/

HTML:

<div id="wrapper">
    <div id="header">
    </div>
    <div id="body">
        <div>
            <div class="portlet">
                <div class="portlet-header">
                    <div class="portlet-title">
                    <h2>BI - External data Control</h2>
                    </div>
                </div>
                <div class="portlet-body">
                    <div>
                        <h3 class="toggle-trigger toggle-trigger-active">External Data Configuration</h3>
                        <div class="toggle-container toggle-container-opened">
                            blah
                        </div>
                        <h3 class="toggle-trigger toggle-trigger-active">Current Notifications</h3>
                        <div class="toggle-container toggle-container-opened">
                            blah
                        </div>
                        <h3 class="toggle-trigger toggle-trigger-active">General Information</h3>
                        <div class="toggle-container toggle-container-opened">
                            blah
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

SCRIPT:

$(document).ready(function() {
    $(".toggle-trigger").click(function(){
        $(this).toggleClass("toggle-trigger-active").next().toggleClass("toggle-container-opened");
        return false; //Prevent the browser jump to the link anchor
    });
});

CSS:

.portlet {
    padding: 5px 5px 5px 5px;
    border: 1px solid black;
}

.portlet-body {
     padding: 5px 5px 5px 5px;
}

.portlet-title {
    background-color: black;
    color: white;
    font-family: Verdana;
    font-size: 13px;
    padding: 6px 6px 6px 6px;
}

.toggle-trigger {
    background-color: lightgrey;
    color: black;
    font-family: Verdana;
    font-size: 13px;
    padding: 6px 6px 6px 6px;
}

.toggle-trigger:before {
    content: "► ";
}

.toggle-trigger-active:before {
    content: "▼ ";
}

.toggle-container {
    font-family: Verdana;
    font-size: 13px;
    padding: 6px 6px 6px 6px;
    display: none;
}

.toggle-container-opened {
    display: block;
}