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

默认的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就好了
posted on 2007-10-25 13:59  一路前行  阅读(2326)  评论(0编辑  收藏  举报