javascript - 收到 document.body 错误。帮助!

标签 javascript html notifications

我有以下 JavaScript 代码:

function createNotification(title, body, canDismiss, callback)
{
    //create the container
    var nContainer = document.createElement("div");
    nContainer.setAttribute("id", "note"+title);
    nContainer.setAttribute("class","nContainer");
    nContainer.className = "nContainer";

    //create the title
    var nTitle = document.createElement("div");
    nTitle.setAttribute("class", "nTitle");
    nTitle.className = "nTitle";
    nTitle.innerHTML = title;

    //if canDismiss is true then add controls
    if (canDismiss)
    {
        var nDismiss = document.createElement("div");
        nDismiss.setAttribute("class", "nDismiss");
        nDismiss.className = "nDismiss";
        nDismiss.innerHTML = "[close]";
        nDismiss.onclick = function(){destroyNotification(title);callback();};
        nTitle.appendChild(nDismiss);

    }

    //append the title to the container
    nContainer.appendChild(nTitle);

    //create the body and append to container
    var nBody = document.createElement("div");
    nBody.setAttribute("class", "nBody");
    nBody.className = "nBody";
    nBody.innerHTML = body;
    nContainer.appendChild(nBody);

    document.body.appendChild(nContainer);

    //fade background
    fadeIt(title);
}

function destroyNotification(title)
{
    //get the specified notification
    var note = document.getElementById("note"+title);

    //remove the notification
    document.body.removeChild(note);

    //unfade the background
    unfadeIt(title)
}

function fadeIt(title)
{
    //create a partially opaque div and append it to the document
    var Fade = document.createElement("div");
    Fade.setAttribute("id", "fade"+title);
    Fade.setAttribute("class","fade");
    Fade.className = "fade";
    document.body.appendChild(Fade);
}

function unfadeIt(title)
{
    //get the specified fade element
    var Fade = document.getElementById("fade"+title);

    //remove the specified fade element
    document.body.removeChild(Fade);
}

我收到 document.body 错误。有人可以帮忙吗?

这是 html:

<html>
<head>
<title></title>
<script langauge="javascript" type="text/javascript" src="notification.js"> </script>
<link rel="stylesheet" type="text/css" href="notification.css" /> 
<script language="javascript" type="text/javascript">

createNotification("The Title", "Some sort of message for our body", true, function(){alert("A Callback");});


</script>
</head>
<body>
</body>
</html>

我从 firebug 得到的错误是:

document.body is null

[Break on this error] document.body.appendChild(nContainer);

最佳答案

您甚至在页面加载之前就执行了函数createNotification。当您调用 body 元素时,它甚至不存在。

调用页面底部的函数或者添加load事件[link] .

关于javascript - 收到 document.body 错误。帮助!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3642387/

相关文章:

javascript - Jasmine: Controller 已定义,但其方法未定义

javascript - JavaScript 中的查找函数

css 扩展边框以包含图像

安卓每日通知

javascript - 如何使用 JavaScript 删除加载 View 中的所有 Chrome 通知?

android - NotificationCompat 无法解析为类型

javascript - 如何将字符串数组相应地分配给子元素?

javascript - jQuery 缩放 div 到父级

html - BeautifulSoup findall 与名称列表没有找到另一个目标之后的目标

html - 我在 Angular 中使用 mdbootstrap 下拉菜单时出现问题