javascript - 弹出提示窗口JS怎么设置弹出一次,假设多弹出也要把前者取消,让后者弹出一次?
PHP中文网
PHP中文网 2017-04-10 17:49:17
[JavaScript讨论组]


这个问题可以看到有两次叠加的现象,弹出提示窗口JS怎么设置弹出一次,假设多弹出也要把前者取消,让后者弹出一次?

function prompt(text,bgcolor) {
    var sbid = document.getElementsByTagName("body")[0];
    var newp = document.createElement("p");
    newp.className="dialog";
    newp.className +=" "+bgcolor;
    newp.innerHTML += "<p>"+text+"</p>";
    sbid.appendChild(newp);
    setTimeout(function(){
        sbid.removeChild(newp);
    },4000);
}
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(6)
大家讲道理

每次弹出的时候都清空一下。然后再渲染

var newp;
function prompt(text,bgcolor) {
    if(newp == null){
        var sbid = document.getElementsByTagName("body")[0];
        newp = document.createElement("p");
        newp.className="dialog";
        newp.className +=" "+bgcolor;
        newp.innerHTML += "<p>"+text+"</p>";
        sbid.appendChild(newp);
        setTimeout(function(){
            newp.parentNode.removeChild(newp);
            newp = null;
        },4000);
    }else{
        newp.parentNode.removeChild(newp);
        newp = null;
        prompt(text,bgcolor);
    }
}
迷茫

弹出弹窗前,先把弹窗内容区域清空,然后再把内容填充进弹窗内容区

怪我咯

给你看看这段代码:

function hello(){      

alert("hello");

}

var id=window.setTimeout(hello,5000);

document.onclick=function(){     

window.clearTimeout(id);

 }
怪我咯
function prompt(text,bgcolor) {
    var sbid = document.getElementsByTagName("body")[0];
    var alert = document.querySelectorAll('.dialog');//得到Dialog列表
    if(alert.length>0){
        sdid.removeChild(alert[0]);
        //如果有多个可以遍历删除
        //但是加了这个,基本上就只有单个了
    }else{
        var newp = document.createElement("p");
        newp.className="dialog";
        newp.className +=" "+bgcolor;
        newp.innerHTML += "<p>"+text+"</p>";
        sbid.appendChild(newp);
        setTimeout(function(){
            sbid.removeChild(newp);
        },4000);
    }
    
}
迷茫

立个flag.

var flag = true;
if(flag) {
    flag = false;
    prompt(...);
}
function prompt(text,bgcolor) {
    clearTimeout(timer);
    var sbid = document.getElementsByTagName("body")[0];
    var newp = document.createElement("p");
    newp.className="dialog";
    newp.className +=" "+bgcolor;
    newp.innerHTML += "<p>"+text+"</p>";
    sbid.appendChild(newp);
    var timer = setTimeout(function(){
        sbid.removeChild(newp);
        flag = true;   //  在这儿还原flag
    },4000);
}

试试

阿神

function prompt(text,bgcolor){

var sbid = document.getElementsByTagName("body")[0];
var newp = document.createElement("p");
newp.className="dialog";
newp.className +=" "+bgcolor;
newp.id="dialog";
newp.innerHTML += "<p>"+text+"</p>";
sbid.appendChild(newp);
if(document.getElementById("dialog")){
    document.getElementById("dialog").onclick = function(){
        this.parentNode.removeChild(document.getElementById("dialog"));
    }
}

}

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号