jQuery .toggle() not working with TRs in IE

前端 未结 13 2438
暗喜
暗喜 2020-12-08 20:43

I am using jQuery\'s toggle() to show/hide table rows. It works fine in FireFox but does not work in IE 8.

.show()/.hide() work fine though

相关标签:
13条回答
  • 2020-12-08 21:07

    I had the same issue, but I found a nice workaround to make toggle/slideToggle work in all browsers.

    <a id="myButton">Click</a>
    <div id="myDiv" display="none">lorem ipsum .... text ....</div>  
    

    and now the JS:

        jQuery("#myButton").click(function () {
      var currentDisplay = jQuery("#myDiv").css("display"); // save the current Display value
      jQuery(#myDiv).slideToggle("fast", function () { // lets do a nice slideToggle
        // this code will run AFTER the sildeToggle is done
        // so if slideToggle worked fine (in FF, chrome, ...) this will have no evect, since the value is already block or none
        // but in case IE 8 runs this script, it will set the css display to the current value
        if (currentDisplay == "none") {
          jQuery(#myDiv).css('display', 'block');
        }else {
          jQuery(#myDiv).css('display', 'none');
            }
        });
        return false;
    });
    

    The result is that slideToggle works fine in all Browsers, except for IE8, in which will look a weird (messed up slide), but it will still work.

    0 讨论(0)
  • 2020-12-08 21:08

    Upgrading to jQuery 1.4 fixes this, whilst I'm at it though, the only "fix" on this page which worked for me was Dough boy's answer:

    $(document).ready(function() {  
      $(".readOnlyRow").hide();  
      $("#readOnlyRowsToggle").click(function() {  
        $(".readOnlyRow").toggle($('.readOnlyRow').css('display') == 'none');  
      });  
    });
    
    0 讨论(0)
  • 2020-12-08 21:13
    $(document).ready(function() {  
        $(".readOnlyRow").hide();  
        $("#readOnlyRowsToggle").click(function() {  
            $(".readOnlyRow").toggle($('.readOnlyRow').css('display') == 'none');  
        });  
    });
    

    There is a jQuery bug that IE8 causes everything to evaluate to true. Try above

    0 讨论(0)
  • 2020-12-08 21:13

    I've noticed this as well. Likely you'll have to toggle a class on each td instead. Haven't tried this solution yet, so let me know!

    For others - this is specific to IE8, not 6 or 7.

    EDIT

    Clearly you didn't try this as evidenced by the -1, as I just did with fine results (in my situation).

    CSS

    tr.hideme td { display: none }

    JS

    $("#view-advanced").click(function() {
        $("tr.hideme td").toggle();
        return false;
    });
    
    0 讨论(0)
  • 2020-12-08 21:14

    Remove the period from your <tr class=".readOnlyRow"><td>row</td></tr>. The syntax for jQuery class selecting is to prepend it with a period, but you don't need it in your HTML code.

    0 讨论(0)
  • 2020-12-08 21:18

    Just encountered this myself -- the easiest solution I've found is to check for:

    :not(:hidden)

    instead of

    :visible

    then act accordingly . .

    0 讨论(0)
提交回复
热议问题