jQuery Hover,显示和可见性CSS在IE7及更低版本中未正确应用

| 有人可以看看下面的小测试案例,并告诉我为什么div(.hide)在IE7和IE6中永远不可见。 (注:我意识到可以使用jQuery的hide()和show()方法,但是我宁愿使用依赖类的基于CSS的解决方案,而不是让jQuery向DOM编写内联样式。)
<html>
<body>
    <script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js\" type=\"text/javascript\"></script>
    <script type=\"text/javascript\">
            $(document).ready(function() {
                $(function() {
                    $(\"table tr\").hover(function() {
                        $(this).addClass(\"hover\");
                    }, function() {
                        $(this).removeClass(\"hover\");
                    });
                });
            });
    </script>

    <style type=\"text/css\">
        .hide {
            visibility: hidden;
            display: block;
            width: 16px;
            height: 16px;
            background-color: #f00;
        }
        .hover .hide {
            visibility: visible;
        }
    </style>

    <table style=\"border-collapse:collapse;\">
        <tr>
            <th class=\"ident\" scope=\"col\">Col1</th>
            <th class=\"fname\" scope=\"col\">Col2</th>
            <th class=\"lname\" scope=\"col\">Col3</th>
        </tr>
        <tr>
            <td>Content</td>
            <td>Content</td>
            <td><div class=\"hide\"></div></td>
        </tr>
        <tr>
            <td>Content</td>
            <td>Content</td>
            <td><div class=\"hide\"></div></td>
        </tr>
        <tr>
            <td>Content</td>
            <td>Content</td>
            <td><div class=\"hide\"></div></td>
        </tr>

        <tr>
            <td>Content</td>
            <td>Content</td>
            <td><div class=\"hide\"></div></td>
        </tr>
    </table>
</body>
</html>
    
已邀请:
不知道这是否能解决您的问题,因为我实际上无法复制您的问题(我只有通过IE9的IE7可供我使用),而是尝试执行以下操作:
<html>
<body>
    <script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js\" type=\"text/javascript\"></script>
    <script type=\"text/javascript\">
            $(document).ready(function() {
                $(function() {
                    $(\"table tr\").hover(function() {
                       $(this).find(\'.redblock\').toggleClass(\'show\');
                    });
                });
            });
    </script>

    <style type=\"text/css\">
        .redblock{
            visibility: hidden;
            display: block;
            width: 16px;
            height: 16px;
            background-color: #f00;
        }
        .show {
            visibility: visible !important;
        }
    </style>

    <table style=\"border-collapse:collapse;\">
        <tr>
            <th class=\"ident\" scope=\"col\">Col1</th>
            <th class=\"fname\" scope=\"col\">Col2</th>
            <th class=\"lname\" scope=\"col\">Col3</th>
        </tr>
        <tr>
            <td>Content</td>
            <td>Content</td>
            <td><div class=\"redblock\"></div></td>
        </tr>
        <tr>
            <td>Content</td>
            <td>Content</td>
            <td><div class=\"redblock\"></div></td>
        </tr>
        <tr>
            <td>Content</td>
            <td>Content</td>
            <td><div class=\"redblock\"></div></td>
        </tr>

        <tr>
            <td>Content</td>
            <td>Content</td>
            <td><div class=\"redblock\"></div></td>
        </tr>
    </table>
</body>
</html>
证明在小提琴中:http://jsfiddle.net/HEkqq/7/     
您的CSS错误:
 .hover .hide {
    visibility: visible;
  }
应该:
 .hover.hide {
    visibility: visible;
  }
另外,您不会在适当的元素上切换类。 看到这个:http://jsfiddle.net/HEkqq/4/
        $(document).ready(function() {
            $(function() {
                $(\"table tr\").hover(function() {
                    $(\'.hide\', this).addClass(\"hover\");
                }, function() {
                    $(\'.hide\', this).removeClass(\"hover\");
                });
            });
        });
另外,您也应该这样:http://jsfiddle.net/HEkqq/6/
$(document).ready(function() {
    $(\"table tr\").hover(function() {
        $(\'.hide\', this).toggleClass(\"hover\");
    });
});
    

要回复问题请先登录注册