自定义JavaScript的alert()弹出框样式
JavaScript原始的alert()弹出框效果很差。
于是想着自定义这个原始alert()的显示样式。
JQuery方式实现代码如下:
<scripttype="text/javascript"src="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"></script>
<scripttype="text/javascript">
(function(){
window.alert=function(text){
//解析alert内容中的换行符
text=text.toString().replace(/\\/g,'\\').replace(/\n/g,'<br/>').replace(/\r/g,'<br/>');
//自定义DIV弹窗
varalertdiv='<divid="alertdiv"style="position:absolute;display:none;overflow:hidden;padding:10px10px8px;top:50%;left:50%;text-align:center;line-height:22px;background-color:#DDE4EE;border:1pxsolid#ccc">'+text+'<br/><inputtype="submit"name="button"id="button"value="确定"style="margin-top:8px;"onclick="$(this).parent().remove();"/></div>';
$(document.body).append(alertdiv);
//设置偏移数值,实现垂直和水平居中
$("#alertdiv").css({
"margin-left":$("#alertdiv").width()/2*(-1)-20,
"margin-top":$("#alertdiv").height()/2*(-1)-20
});
//显示
$("#alertdiv").show();
};
})();
</script>
<inputtype="submit"name="button"id="button"value="点击"onclick='alert("这是alert弹窗\n支持\\n换行符")'/>热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语