firefox中的jquery切换问题

时间:2012-01-29 12:15:31

标签: php jquery slider toggle

以下代码在Chrome中运行良好,但在Firefox中无法正常运行。

在Chrome中

展开隐藏/显示链接会在展开时正确显示向上箭头,在折叠时显示向下箭头

在Firefox中:

展开隐藏/显示链接会在展开时正确显示向上箭头,但折叠后仍会显示向上箭头。

我做错了什么?

由于

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    var $divView = $('div.view');
    var innerHeight = $divView.removeClass('view').height();
    $divView.addClass('view');      
    $('div.slide').click(function() {

        // Update the HTML in this element
        var slideHtml = $(this).html();

        // Switch between show/hide
        if (slideHtml.localeCompare('Hide / Show <img src="images/arrow_up.png" />') < 0)
           $(this).html('Hide / Show <img src="images/arrow_up.png" />');
        else
           $(this).html('Hide / Show <img src="images/arrow_down.png" />');
        $('div.view').animate({
          height: (($divView.height() == 90)? innerHeight  : "90px")
        }, 500);
        return false;
    });
});
</script>
<div class="view">
   <ul>
    <li>text here</li>
    <li>text here</li>
    <li>text here</li>
    <li>text here</li>
    <li>text here</li>
    <li>text here</li>
   </ul>
</div>
<div class="slide">Hide / Show <img src="images/arrow_down.png" /></div>

2 个答案:

答案 0 :(得分:0)

您应该使用toggle()它更简单,它可以避免这一切。

我在这里给你举了一个例子:http://jsfiddle.net/MVnbM/

HTML:

<a href="javascript:;" class="clickMe">Click me</a>
<div class="arrowUp">Up</div>
<div class="arrowDown">Down</div>

JS:

$('.arrowUp').hide();
$('a.clickMe').click(function() {
   $('.arrowUp, .arrowDown').toggle(); 
});

此处修改了您的代码:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    var $divView = $('div.view');
    var innerHeight = $divView.removeClass('view').height();
    $divView.addClass('view');      
    $('.arrowUp').hide();
    $('div.slide').click(function() {
        $('.arrowUp, .arrowDown').toggle(); 
    });
});

<div class="view">
   <ul>
    <li>text here</li>
    <li>text here</li>
    <li>text here</li>
    <li>text here</li>
    <li>text here</li>
    <li>text here</li>
   </ul>
</div>
<div class="slide">Hide / Show <img src="images/arrow_down.png" class="arrowUp" /><img src="images/arrow_down.png" class="arrowDown" /></div>

答案 1 :(得分:0)

使用toggletoggleClass的此变体怎么样? http://jsfiddle.net/347pg/