在弹出窗口之前显示背景颜色

Display Background Color before Popup?

本文关键字:显示 背景 颜色 窗口      更新时间:2023-09-26

我想在警报弹出之前设置一个绿色的背景色。我该怎么做呢?

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <head><meta charset="utf-8" />
    <style>
html, body{
        background: #0f0;
}
    </style>
    </head>
    <title>Hi There!</title>
    <script LANGUAGE='JavaScript'>
            window.alert('Hello There!')
    </script>

我以为这样就可以了,但是警告会弹出在一个白页面

使用

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta charset="utf-8" />
        <title>Hi There!</title>
        <style>
            body{
                    background: #0f0;
            }
        </style>
        <script>
            function showMessage() {
                window.alert('Hello There!')
            }
        </script>
    </head>
    <body onload="showMessage();">
        ..
    </body>
</html>

注意body标签中的onload属性。该属性的值是在页面完全加载后执行的JavaScript代码(CSS,图像等)。


如果你真的想要延迟,把showMessage函数改成这样:

function showMessage() {
    var secondsDelay = 5; //5 seconds
    setTimeout(function() {
        window.alert('Hello There!');
    }, secondsDelay * 1000);
}