[英]Strange behavior in JavaScript
我有2個元素-“ span”(命名為“ divLikedX”)和“ a”(命名為“ aLikeX”)。 我有以下javascript(點擊“ a”時發生):
function CommentLike(CommentID, aLink) {
if (CommentID != null && CommentID > 0)
$.post("/Home/LikeComment", { CommentID: CommentID },
function () {
//alert($("#divLiked" + CommentID).is(':visible'));
/*alert($(aLink).text());*/if ($("#divLiked" + CommentID).is(':hidden')) {
$("#divLiked" + CommentID).show();
$("#aLike" + CommentID).text('Unlike');
} else {
$("#divLiked" + CommentID).hide();
$("#aLike" + CommentID).text('Like');
}
});
};
如果我刪除$("#aLike" + CommentID).text('Unlike');
和$("#aLike" + CommentID).text('Like');
字符串我得到正確的行為。 但是,使用這些字符串,它只能在前兩次單擊中正常工作,之后它alert($("#divLiked" + CommentID).is(':visible')) == "true"
。 為什么?
您似乎並不是唯一一個有此問題的人:cf http://forum.jquery.com/topic/hidden-visible-broken-in-ie8
當display:none元素附近有可見元素時,問題似乎會出現在IE8中。 這似乎愚弄了檢測:visible的jQuery算法。
我可以建議您使用類而不是:visible和:hidden進行測試:
if ($("#divLiked" + CommentID).hasClass('like')) {
$("#divLiked" + CommentID).removeClass('like').show();
$("#aLike" + CommentID).text('Unlike');
} else {
$("#divLiked" + CommentID).addClass('like').show();
$("#aLike" + CommentID).text('Like');
}
我希望這能幫到您,
傑羅姆·瓦格納(Jerome Wagner)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.