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

本文介绍了一种使用HTML和JavaScript自定义Alert窗口样式的方法,通过创建更详细的提示信息并加入操作按钮,增强了用户体验。

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

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

</Script>
一切就绪,现在我们只需要按原来的方法调用Alert就好了。
<script language="JavaScript" type="text/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窗口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值