利用Javascript的“函数重载”实现自定义Alert样式

本文介绍了一种自定义HTML和JavaScript实现的Alert对话框方法,通过修改样式和添加操作按钮,使弹窗更具交互性和美观性。文章详细展示了如何使用HTML表格创建样式丰富的消息提示框,并通过JS函数重载,将自定义的Alert集成到现有系统中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

默认的Alert只有一个简单的对话框,而且无法自定义样式!有时候弹出的提示需要很详尽,并且显得有些冗长,那么如何让用户直接看到重点信息呢?因为系统自带的Alert无法编辑样式所以我们只好自己重新定义一个了!
先看看我经常使用的信息提示样式吧!
数据传递错误
 [软件编号]:必须是0-9的阿拉伯数字!
这其实就是一个普通的Html表格,里面加上2个操作按钮!
 完整的HTML代码如下:
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
  
<tr>
    
<td align="center" bgcolor="#CCCCCC"><span class="STYLE1">操作提示</span></td>
  
</tr>
  
<tr>
    
<td bgcolor="#FFFFFF">&nbsp;</td>
  
</tr>
  
<tr>
    
<td align="center" bgcolor="#FFFFFF"><input type="button" name="Submit" value="返回" onclick="window.history.go(-1)"/>
    
<input type="button" name="Submit2" value="关闭"  onclick="window.close()"/></td>
  
</tr>
</table>
你可以把这些代码改成任何你喜欢的样式,但是最好留下返回和关闭按钮。PS:没有操作按钮就没法返回上一页拉!
接下来把这些表格代码转换成JS,并且利用JS的“函数重载”重新定义系统自带的Alert 窗口。
具体代码如下:
<Script Language="JavaScript">
window.alert
=function (txt)
...{
    document.write (
'<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">');
    document.write (
'  <tr>');
    document.write (
'    <td align="center" bgcolor="#CCCCCC"><span class="STYLE1">操作提示</span></td>');
    document.write (
'  </tr>');
    document.write (
'  <tr>');
    document.write (
'    <td bgcolor="#FFFFFF">'+txt+'</td>');
    document.write (
'  </tr>');
    document.write (
'  <tr>');
    document.write (
'    <td align="center" bgcolor="#FFFFFF"><input type="button" name="Submit" value="返回" onclick="window.history.go(-1)"/>');
    document.write (
'    <input type="button" name="Submit2" value="关闭"  onclick="window.close()"/></td>');
    document.write (
'  </tr>');
    document.write (
'</table>');
}

</Script>
一切就绪,现在我们只需要按原来的方法调用Alert就好了

转载于:https://www.cnblogs.com/zhangpengshou/archive/2007/10/25/937233.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值