JS使用警告如何实现一个弹框效果

  

JS使用警告如何实现一个弹框效果?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

具体内容如下

先上图:

 JS使用警告如何实现一个弹框效果

警报。js代码:

 var myAlert={
  栏:函数(alertContent) {
  var windowWidth=window.innerWidth;
  windowHeight=window.innerHeight;
  alertContainer=document.createElement (“div");
  alertContainer。id=癿yAlertBox";
  alertContainer.style.cssText=拔恢?绝对的,左:0 px;: 0 px;宽度:100%;z - index: 9999;“;
  alertContainer.style。身高=windowHeight +“px";
  alertOpacity=document.createElement (“div");
  alertOpacity.style.cssText=拔恢?绝对的,左:0 px;: 0 px;宽度:100%;背景:# 000;不透明度:0.5;z - index: 9999;“;
  alertOpacity.style。身高=windowHeight +“px";
  alertContainer.appendChild (alertOpacity)
  alertMainBox=document.createElement (“div");
  alertMainBox.style.cssText=拔恢?绝对的,宽度:200 px;高度:200 px;行高:200 px; text-align:中心;背景:绿色;z - index: 10000;“
  alertMainBoxLeft=(windowwidth - 200)/2;
  alertMainBoxTop=(windowheight - 200)/2;
  alertMainBox.style。左=alertMainBoxLeft +“px";
  alertMainBox.style。顶级=alertMainBoxTop +“px";
  alertMainBox。innerHTML=alertContent;
  alertContainer.appendChild (alertMainBox);
  alertClose=document.createElement (“div");
  alertClose。id=癱loseBox";
  alertClose.style。cssText=拔恢?绝对;右:0 px;: 0 px;宽度:30 px;高度:30 px;背景:红色,光标:pointer";
  alertMainBox.appendChild (alertClose);
  document.body.appendChild (alertContainer);
  closeButton=. getelementbyid (“closeBox");
  console.log (closeButton)
  closeButton。onclick=function () {
  document.body.removeChild (. getelementbyid (“myAlertBox"));
  }
  }
  }

以下是具体要用时的代码:

& lt; !DOCTYPE HTML>   & lt; html>   & lt; head>   & lt;元http-equiv=癱ontent-type"内容=皌ext/html;charset=utf-8"比;   & lt; title> Study   & lt;/head>   & lt;脚本类型=拔谋?javascript"charset=皍tf-8"src=https://www.yisu.com/zixun/" alert.js ">   身体<>   <脚本type=" text/javascript“charset=" utf - 8 ">   myAlert.alertbox(“这是自定义警报框”);>   

用法很简单,引入alert.js文件,要用时直接myAlert.alertbox(“内容“);和使用警报一样。

看完上述内容,你们掌握JS使用警告如何实现一个弹框效果的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

JS使用警告如何实现一个弹框效果