【发布时间】:2012-08-13 04:18:40
【问题描述】:
我有一个我似乎无法识别的问题,我的代码在 Firefox 和 Chrome 中运行良好,但在 IE 中失败。
我有以下元素的序列:
<tbody id="tbod161-1__" isloaded="true" style="display: none;"></tbody>
<tbody id="tbod162-2__" isloaded="true"></tbody>
我正在尝试创建一个 jQuery cookie,它存储元素是否可见。
function RememberClickedState() {
$('.ms-listviewtable tbody[id^="tbod"]').each(function(){
tid = $(this).attr('id');
var tvisible = ($(this).attr('style') == undefined || $(this).attr('style').indexOf('display: none;') == -1);
var strVisible;
if( tvisible == true)
{
strVisible = "true";
}
if( tvisible == false)
{
strVisible = "false";
}
items += tid+':'+strVisible+';'
})
$.cookie("itemListState", items);
}
当我检索值时:
string = $.cookie("itemListState");
alert(string);
...在 IE 中所有的 ID 都设置为“true”,这意味着这些值被错误地写入了 cookie。但是,此代码在某些 ID 正确设置为 false 的 Ff / Chrome 中运行时可以完美运行。
我错过了什么? 谢谢,
【问题讨论】:
-
使用 jQuery,您可以说
$(this).is(':visible')... 而且您不需要if/else将布尔值转换为字符串 - 只需在字符串连接表达式中直接使用它即可。跨度> -
尝试检查 $(this).css('display')=='none' 而不是读取“style”属性的值。
标签: javascript jquery jquery-cookie