jQuery .toggle() not working with TRs in IE

2019-01-17 10:50发布

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.

slideToggle() does not work in IE either - it shows for a split second then disappears again. Works fine in FireFox.

My HTML looks similar to this

<a id="readOnlyRowsToggle">Click</a>  
<table>  
  <tr><td>row</td></tr>  
  <tr><td>row</td></tr>  
  <tr class="readOnlyRow"><td>row</td></tr>  
  <tr class="readOnlyRow"><td>row</td></tr>  
  <tr class="readOnlyRow"><td>row</td></tr>  
</table>

JavaScript

$(document).ready(function() {  
    $(".readOnlyRow").hide();  
    $("#readOnlyRowsToggle").click(function() {  
        $(".readOnlyRow").toggle();  
    });  
});  

13条回答
等我变得足够好
2楼-- · 2019-01-17 11:11

Looks like this has been fixed in JQuery 1.4.

查看更多
姐就是有狂的资本
3楼-- · 2019-01-17 11:14

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.

查看更多
该账号已被封号
4楼-- · 2019-01-17 11:16

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;
});
查看更多
Rolldiameter
5楼-- · 2019-01-17 11:19

I've experienced this same error on tr's in tables. I did some investigation using IE8's script debugging tool.

First I tried using toggle:

$(classname).toggle();

This works in FF but not IE8.

I then tried this:

if($(classname).is(":visible"))//IE8 always evaluates to true.
     $(classname).hide();
else
     $(classname).show();

When I debugged this code, jquery always thought it was visible. So it would close it but then it would never open it back.

I then changed it to this:

var elem = $(classname)[0];
if(elem.style.display == 'none')
     $(classname).show();
else
{
     $(classname).hide();               
}

That worked fine. jQuery's got a bug in it or maybe my html's a little screwy. Either way, this fixed my issue.

查看更多
戒情不戒烟
6楼-- · 2019-01-17 11:19

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');  
  });  
});
查看更多
成全新的幸福
7楼-- · 2019-01-17 11:20

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

:not(:hidden)

instead of

:visible

then act accordingly . .

查看更多
登录 后发表回答