JavaScript原始的alert()弹出框效果很差。
于是想着自定义这个原始alert()的显示样式。
JQuery方式实现代码如下:
(function () {
window.alert = function (text) {
//解析alert内容中的换行符
text = text.toString().replace(/\\/g, '\\').replace(/\n/g, '
').replace(/\r/g, '
');
// 自定义DIV弹窗
var alertdiv = '
' + text + '';
$(document.body).append(alertdiv);
// 设置偏移数值,实现垂直和水平居中
$("#alertdiv").css({
"margin-left": $("#alertdiv").width() / 2 * (-1) - 20,
"margin-top": $("#alertdiv").height() / 2 * (-1) - 20
});
// 显示
$("#alertdiv").show();
};
})();