简体   繁体   English

jQuery .hide()和.show()无法正常工作

[英]jQuery .hide() and .show() not working

I have this code generate dynamically using php code:- 我有这段代码使用php代码动态生成:-

<div class="mailList" id="M_6">
    <div class="mailListHeader" id="H_6">
        <img style="float:right; display:none;" class="loaderIMG" id="LOADER_6" src="images/preloader.gif">
        Sent by <strong>Admin</strong> on <strong>Oct 03 2013 02:53 PM</strong> to <strong>Received Response</strong> for Quarter <strong>3</strong> Year <strong>2013</strong>.<br>
        Subject: <strong>Test Mail</strong><br>
    </div>

    <div class="mailListContent" id="C_6">
        <div class="closeContent" id="CC_6">Close [x]</div>
        <span id="SPAN_6"></span>
    </div>

    <div class="mailListFooter" id="F_6">
        <span class="mailContentBtn" id="MCBTN_6" style="font-size:11px; color:#09C; cursor:pointer;">
            View Content
        </span>
        <span class="mailListBtn" id="MLBTN_6" style="float:right; font-size:11px; color:#C06; cursor:pointer;">
            Successfull-[0] Failed-[4]
        </span>
    </div>
</div>

Then, user can click View Content or Successfull-[0] Failed-[4] that will make a ajax request than display result in div mailListContent. 然后,用户可以单击“ 查看内容”或“ 成功-[0]失败-[4]” ,该请求将发出ajax请求,而不是在div mailListContent中显示结果。 Below is code for the jquery ajax request:- 以下是jquery ajax请求的代码:-

$(".mailContentBtn, .mailListBtn").click(function(){
    var currentId = $(this).attr('id');
    currentId = currentId.split("_");
    var actualId = currentId[1];

    if($("#C_"+actualId).is(":visible")) {
        $("#C_"+actualId).hide("slow","swing");
    }
    $("img#LOADER_"+actualId).show();

    if(currentId[0]=="MCBTN") {
        var dataString ="action=getMailContentByID&mailID="+actualId;  
    } else {
        var dataString ="action=getMailListByID&mailID="+actualId;
    }

    $.ajax({
        type: "POST",
        url: "include/getMail.php",
        data: dataString,
        cache: false,
        async: false,
        success: function(html) { 
            $("#SPAN_"+actualId).empty();
            $("#SPAN_"+actualId).append(html);

            $("#C_"+actualId).show("slow","swing");
            $("img#LOADER_"+actualId).hide();
        } 
    });
});

The request and the events works fine, the problem is every time user click at View Content or Successfull-[0] Failed-[4] the loading image is not display. 该请求和事件运行良好,问题在于用户每次单击“ 查看内容”成功-[0]失败-[4]时 ,均不会显示正在加载的图像。 As you can see, I give a unique ID for every loading image than only 1 loading image will display on clik. 如您所见,我为每个加载图像指定了唯一的ID,而不是在clik上仅显示1个加载图像。 There is no error in inspect code in Google Chrome. 在Google Chrome浏览器中检查代码没有错误。 How can I solve this? 我该如何解决?

Thank you. 谢谢。

In your call to $.ajax, change the "async" option to "true". 在对$ .ajax的调用中,将“异步”选项更改为“真”。 Because in your case, the $.ajax is blocking the ui thread in displaying the loading image as it is executed synchronously. 因为在您的情况下,$。ajax阻止ui线程显示同步执行的加载图像。

You have missed: 您错过了:

$(document).ready(function () {
});

try this: 尝试这个:

<script>
        $(document).ready(function () {
            $(".mailContentBtn, .mailListBtn").click(function () {
                var currentId = $(this).attr('id');
                currentId = currentId.split("_");
                var actualId = currentId[1];

                if ($("#C_" + actualId).is(":visible"))
                    $("#C_" + actualId).hide("slow", "swing");

                $("img#LOADER_" + actualId).show();

                if (currentId[0] == "MCBTN") {
                    var dataString = "action=getMailContentByID" +
                            "&mailID=" + actualId;
                }
                else {
                    var dataString = "action=getMailListByID" +
                            "&mailID=" + actualId;
                }

                $.ajax({
                    type: "POST",
                    url: "include/getMail.php",
                    data: dataString,
                    cache: false,
                    async: false,
                    success: function (html) {
                        $("#SPAN_" + actualId).empty();
                        $("#SPAN_" + actualId).append(html);

                        $("#C_" + actualId).show("slow", "swing");
                        $("img#LOADER_" + actualId).hide();
                    }
                });
            });
        })


    </script>

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

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