$(document.body).append()似乎在IE中不起作用

时间:2010-08-19 05:13:27

标签: jquery internet-explorer append document-body

当我的用户会话到期并且在母版页下引用默认页面时,我将用户重定向到我的default.aspx。我在Twitter上显示了一条通知,说明Session Expired发生了什么,它在firefox和谷歌浏览器中工作得很好但在IE中却没有。我得到Internet Explorer cannot open site-http://localhost:1335/order/Default.aspx?Sid=1 Operation aborted

我用谷歌搜索了这一点,发现身体标签前$(document.body).append()是问题,如果我将脚本移到我的页面底部,我的通知在任何浏览器中都不起作用。

这是我的母版页

<head runat="server">
    <title></title>

    <script src="Javascript/Jquery1.4.js" type="text/javascript"></script>

    <script type="text/javascript">
        function topBar(message) {
            $("#alertmsg").remove();
            var $alertdiv = $('<div id = "alertmsg"/>');
            $alertdiv.text(message);
            $alertdiv.bind('click', function() {
                $(this).slideUp(200);
            });
            $(document.body).append($alertdiv);
            $("#alertmsg").slideDown("slow");
            setTimeout(function() { $alertdiv.slideUp(200); }, 5000);
        }
    </script>

    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>

我的topbar函数在我的default.aspx页面中调用,

protected void Page_Load(object sender, EventArgs e)
{
    Int64 id = GetId(Request.RawUrl.ToString());
  if (id == 1)
  {
    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "json", "topBar('Session Expired');", true);
  }
}

1 个答案:

答案 0 :(得分:1)

我找不到调用topBar的地方,但如果我从$(document).ready()调用它,它就像IE8中的魅力一样:

<script type="text/javascript"> 
    function topBar(message) { 
        $("#alertmsg").remove(); 
        var $alertdiv = $('<div id = "alertmsg"/>'); 
        $alertdiv.text(message); 
        $alertdiv.bind('click', function() { 
            $(this).slideUp(200); 
        }); 
        $(document.body).append($alertdiv); 
        $("#alertmsg").slideDown("slow"); 
        setTimeout(function() { $alertdiv.slideUp(200); }, 5000);
    }

    $(document).ready(function() {
        topBar("Hello world!");
    });
</script> 

如果在调用$(window).load()之前需要加载图形和/或其他重元素,也可以使用topBar()

$(window).load(function() {
    topBar("Hello world!");
});

希望它有所帮助。

修改


也许this可以提供一些帮助?基本上你可以做这样的事情:

ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "json",  
    @"$(document).ready(function(){   
             topBar("Hello world!");
             }); 
        });", true); 

查看链接中的answer,因为他提供了几个替代方案