问题
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();
});
});
回答1:
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.
回答2:
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');
});
});
回答3:
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.
回答4:
I found that while this does not work in IE8
$('.tableRowToToggle').toggle();
but this does work:
$('.tableRowToToggle').each(function(){this.toggle()});
Got the idea from the link jAST posted here before
回答5:
I fixed this problem by hiding children of the TR element.
toggleTr($(".toggletr"));
function toggleTr(el) {
$(el).children('td').each(function() {
$(this).toggle();
});
}
This seems to be ok for FF3.5/FF3/IE8/IE7/IE6/Chrome/Safari.
回答6:
Just encountered this myself -- the easiest solution I've found is to check for:
:not(:hidden)
instead of
:visible
then act accordingly . .
回答7:
Looks like this has been fixed in JQuery 1.4.
回答8:
Tables are an interesting piece of html, they don't follow normal rules as other elements.
basically tables display as tables, there are special rules for tables but this was not dealt with properly in older browsers because of the roller coaster of craziness that was the browser wars.
Essentially in older browsers tables display properties were minimal and the flow was left largely undocumented so instead of altering predefined values for the table/tr/td tag it is best to instead add and remove the following class and simply toggle the class itself on and off on the attribute of the table/tr/td.
.tableHide {
display: none;
}
<table>
<tr><td> Visible content</td></tr>
<tr><td> Visible content</td> <td class="**tableHide**"> **Hidden Content**</td></tr>
</table>
The reason for this is so that it is a separate class being used to toggle the display therefore nothing on the table itself is being changed nor do any of the tables properties need to be altered, display and any related layout properties are preserved even if the browser doesn't make that easy behind the scenes.
回答9:
$(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
回答10:
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.
回答11:
I have noticed that toggle may not work for iphone if you include jquery mobile. If you run toggle (.ready) before jquery mobile is loaded then its fine.
回答12:
because you are having them click an <a>, you need the function to return false.
回答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;
});
来源:https://stackoverflow.com/questions/975153/jquery-toggle-not-working-with-trs-in-ie