简体   繁体   English

JavaScript中的奇怪行为

[英]Strange behavior in JavaScript

I have 2 elements - "span" (named "divLikedX") and "a" (named "aLikeX"). 我有2个元素-“ span”(命名为“ divLikedX”)和“ a”(命名为“ aLikeX”)。 I have the following javascript (occurs clicking by "a"): 我有以下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');
                }
            });
        };

If I remove $("#aLike" + CommentID).text('Unlike'); 如果我删除$("#aLike" + CommentID).text('Unlike'); and $("#aLike" + CommentID).text('Like'); $("#aLike" + CommentID).text('Like'); strings I get the correct behavior. 字符串我得到正确的行为。 But with these strings it works correctly only first 2 clicks, after it alert($("#divLiked" + CommentID).is(':visible')) == "true" always. 但是,使用这些字符串,它只能在前两次单击中正常工作,之后它alert($("#divLiked" + CommentID).is(':visible')) == "true" Why? 为什么?

you do not seem to be the only one with the issue : cf http://forum.jquery.com/topic/hidden-visible-broken-in-ie8 您似乎并不是唯一一个有此问题的人:cf http://forum.jquery.com/topic/hidden-visible-broken-in-ie8

The problems seems to append in IE8 when a display:none element has visible elements nearby. 当display:none元素附近有可见元素时,问题似乎会出现在IE8中。 This seems to fool the jquery algorithm that detects :visible. 这似乎愚弄了检测:visible的jQuery算法。

I can advice you to test with a class instead of :visible and :hidden : 我可以建议您使用类而不是: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');
}

I hope this will help you, 我希望这能帮到您,

Jerome Wagner 杰罗姆·瓦格纳(Jerome Wagner)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM