如何取代javascript中alert方法

来源:百度知道 编辑:UC知道 时间:2024/06/04 06:56:22
有时候如何一个
<input type="text">为空的时候我显示一个alert警告框,现在我想出错时候,错误提示出现在一个<td></td>里怎么办??
能不能举个例子

有2中方法:

第一种:通过div层模拟alert方法

第二种:覆盖window.alert方法

对于一般的应用,这里推荐第二种方式。

示例:

var j_oldAlert = window.alert;//保修原来的alert函数
window.alert = function (msg) {//覆盖原来的alert函数
   var newMsg = msg + '新消息';//修改消息
   j_oldAlert(newMsg);//弹出消息
}

<input type="text" id="entry" />
<td id="output"></td>

function reportErr(){
if(document.getElementById("entry").value==""){
var output=document.getElementById("output");
output.innerHTML="小泉草NM!";//显示错误信息
}
}
//覆盖window.alert()方法
window.alert=reportErr;

这样就可以 了

方法如下:
function reportErr(){
if(document.getElementById("entry").value==""){
var output=document.getElementById("output"