重写confirm方法js函数返回true false和false,结果后面有用到

所有回答(5)
&a href="javascript:void(0);" id="del"&删除&/a&
$("#del").on("click",function(){
  if(confirm("你确定删除?")){
    // 如果确定,你这里的代码就开始执行。
暂时都是使用的回调,期待能中断操作的代码~
园豆:34160
what do you want to do? I can not understand.
没有完美的方法,目前大多数框架实现的都是基于html元素,比如用jQuery UI的具体做法:
html元素自身设置一个变量,用于保存点击结果
html元素点击时,返回false,再弹出jQuery UI Dialog
把jQuery UI Dialog点击结果保存到元素
如果点击结果为true,重新触发元素点击事件并返回true
&大体上是这样模拟confirm操作,还需要针对不同的情况做额外的处理,比如有些html元素是href="javascript:...",还有些元素是onclick="__doPostback..."。
具体可以看我的博客:&里面confirm部分方法的实现&
园豆:1139
confirm('') ? trueFun : falseF
&&&您需要以后才能回答,未注册用户请先。1301人阅读
JavaScript(30)
JQuery(48)
OnClientClick=&javascript: var a=confirm('你确定要删除吗?');if(!a){return false)}& return false的意思是表示:取消冒泡,取消默认事件
OnClick=&javascript: var a=confirm('你确定要删除吗?');if(!a){return false)}&
&html xmlns=&http://www.w3.org/1999/xhtml&&
&head runat=&server&&
&meta http-equiv=&Content-Type& content=&text/ charset=utf-8& /&
&title&&/title&
&form id=&form1& runat=&server&&
&div onclick=&test()&&ss&/div&
&script type=&text/javascript&&
function test(e) {
var a = confirm(&确定吗?&);
alert(&点击了确定&);
else { alert(&点击了取消&);return false }
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:593595次
积分:10305
积分:10305
排名:第1595名
原创:449篇
转载:22篇
评论:61条
(2)(4)(2)(2)(4)(6)(1)(2)(3)(8)(4)(8)(12)(8)(6)(5)(19)(10)(11)(5)(19)(7)(12)(29)(7)(2)(19)(12)(12)(9)(26)(23)(32)(6)(14)(23)(15)(21)(1)(5)(13)(13)(15)(16)4976人阅读
软件开发(10)
window.confirm 参数就只有一个。显示提示框的信息。按确定,返回true;按取消返回false。&& &&& & &script&&& & var bln = window.confirm("确定吗?");&& & alert(bln)&& & &/script&&&&
& window.alert参数,只有一个,显示警告框的信息;无返回值。&& &&& & &script&&& & window.alert("确定。")&& & &/script&&&
window.prompt参数,有两个,第一个参数,显示提示输入框的信息。第二个参数,用于显示输入框的默认值。返回,用户输入的值。&& &&& & &script&&& & var str = window.prompt("请输入密码","password")&& & alert(str);&& & &/script&
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:111745次
积分:1387
积分:1387
排名:千里之外
原创:22篇
转载:26篇
评论:12条
(1)(1)(3)(1)(4)(2)(1)(2)(2)(5)(2)(1)(1)(1)(1)(1)(5)(1)(6)(1)(6)21:32 提问
javaScript中返回true和false的问题,求大神解答!!
Created by IntelliJ IDEA.
User: TopbeCoder5
Time: 20:44
To change this template use File | Settings | File Templates.
&%@ page contentType="text/charset=UTF-8" language="java" %&
&title&测试alertMsg&/title&
&link href="css/alertMsg.css" rel="stylesheet" type="text/css"/&
&script type="text/javascript"&
function alertMsg(msg, mode) { //mode为空,即只有一个确认按钮,mode为1时有确认和取消两个按钮
msg = msg || '';
mode = mode || 0;
var top = document.body.scrollTop || document.documentElement.scrollT
var isIe = (document.all) ? true :
var isIE6 = isIe && !window.XMLHttpR
var sTop = document.documentElement.scrollTop || document.body.scrollT
var sLeft = document.documentElement.scrollLeft || document.body.scrollL
var winSize = function () {
var xScroll, yScroll, windowWidth, windowHeight, pageWidth, pageH
// innerHeight获取的是可视窗口的高度,IE不支持此属性
if (window.innerHeight && window.scrollMaxY) {
xScroll = document.body.scrollW
yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight & document.body.offsetHeight) { // all but Explorer Mac
xScroll = document.body.scrollW
yScroll = document.body.scrollH
} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
xScroll = document.body.offsetW
yScroll = document.body.offsetH
if (self.innerHeight) {
// all except Explorer
windowWidth = self.innerW
windowHeight = self.innerH
} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
windowWidth = document.documentElement.clientW
windowHeight = document.documentElement.clientH
} else if (document.body) { // other Explorers
windowWidth = document.body.clientW
windowHeight = document.body.clientH
// for small pages with total height less then height of the viewport
if (yScroll & windowHeight) {
pageHeight = windowH
pageHeight = yS
// for small pages with total width less then width of the viewport
if (xScroll & windowWidth) {
pageWidth = windowW
pageWidth = xS
'pageWidth': pageWidth,
'pageHeight': pageHeight,
'windowWidth': windowWidth,
'windowHeight': windowHeight
//alert(winSize.pageWidth);
var styleStr = 'top:0;left:0;position:z-index:10000;background:#666;width:' + winSize.pageWidth + 'height:' + (winSize.pageHeight + 30) + '';
styleStr += (isIe) ? "filter:alpha(opacity=80);" : "opacity:0.8;"; //遮罩层DIV
var shadowDiv = document.createElement('div'); //添加阴影DIV
shadowDiv.style.cssText = styleS //添加样式
shadowDiv.id = "shadowDiv";
//如果是IE6则创建IFRAME遮罩SELECT
if (isIE6) {
var maskIframe = document.createElement('iframe');
maskIframe.style.cssText = 'width:' + winSize.pageWidth + 'height:' + (winSize.pageHeight + 30) + 'position:visibility:z-index:-1;filter:alpha(opacity=0);';
maskIframe.frameborder = 0;
maskIframe.src = "about:blank";
shadowDiv.appendChild(maskIframe);
document.body.insertBefore(shadowDiv, document.body.firstChild); //遮罩层加入文档
var styleStr1 = 'display:position:_position:left:' + (winSize.windowWidth / 2 - 125) + 'top:' + (winSize.windowHeight / 2 - 70) + '_top:' + (winSize.windowHeight / 2 + top - 150) + ''; //弹出框的位置
var alertBox = document.createElement('div');
var alertTitle = document.createElement('div');
alertTitle.id = 'alertTitle';
alertBox.id = 'alertMsg';
alertBox.style.cssText = styleStr1;
//创建弹出框里面的内容P标签
var alertMsg_info = document.createElement('P');
alertMsg_info.id = 'alertMsg_info';
alertMsg_info.innerHTML =
alertTitle.innerHTML = '提示信息!';
alertBox.appendChild(alertTitle);
alertBox.appendChild(alertMsg_info);
//创建按钮
var styleStr2 = 'display:position:_position:left:' + (winSize.windowWidth / 2 - 125) + 'top:' + (winSize.windowHeight / 2 + 40) + '_top:' + (winSize.windowHeight / 2 + top - 150) + ''; //弹出框的位置
var alertBottom = document.createElement('div');
alertBottom.id = 'alertBottom';
alertBottom.style.cssText = styleStr2;
if (mode === 1) {
var btn1 = document.createElement('a');
btn1.id = 'alertMsg_btn1';
btn1.href = 'javas' + 'cript:void(0)';
btn1.innerHTML = '&cite&确定&/cite&';
btn1.onclick = function () {
document.body.removeChild(alertBox);
document.body.removeChild(shadowDiv);
document.body.removeChild(alertBottom);
//document.getElementById("logout").click();
alertBottom.appendChild(btn1);
var btn2 = document.createElement('a');
btn2.id = 'alertMsg_btn2';
btn2.href = 'javas' + 'cript:void(0)';
btn2.innerHTML = '&cite&取消&/cite&';
btn2.onclick = function () {
document.body.removeChild(alertBox);
document.body.removeChild(shadowDiv);
document.body.removeChild(alertBottom);
var btn2 = document.createElement('a');
btn2.id = 'alertMsg_btn2';
btn2.href = 'javas' + 'cript:void(0)';
btn2.innerHTML = '&cite&确定&/cite&';
btn2.onclick = function () {
document.body.removeChild(alertBox);
document.body.removeChild(shadowDiv);
document.body.removeChild(alertBottom);
alertBottom.appendChild(btn2);
document.body.appendChild(alertBox);
document.body.appendChild(alertBottom);
function confirmMsg() {
if (alertMsg("测试", 1)) {
alert("点击了确定");
alert("点击了取消");
&input type="button" value="Test the alert"
onclick="return confirmMsg('test confirm');"&
为什么一点botton就直接运行了false啊??
按赞数排序
alertMsg没有return,默认返回的undefined,对于if来说是false
目测,alertMsg是异步执行的,你需要将它的onclick暴露出来,把你的确定取消的判断写在onclick里面。
直接断点调试一下
你这个是层模拟的,和系统的alert不一样,无法挂起js代码的执行。需要回掉函数,再点击你的按钮时执行回掉函数做需要的操作
帮你大概改了下。
&head&&meta http-equiv="content-type" content="text/charset=gb2312" /&
&title&测试alertMsg&/title&
&link href="css/alertMsg.css" rel="stylesheet" type="text/css"/&
&script type="text/javascript"&
//增加callback回调
function alertMsg(msg, mode,callback) { //mode为空,即只有一个确认按钮,mode为1时有确认和取消两个按钮
msg = msg || '';
mode = mode || 0;
var top = document.body.scrollTop || document.documentElement.scrollT
var isIe = (document.all) ? true :
var isIE6 = isIe && !window.XMLHttpR
var sTop = document.documentElement.scrollTop || document.body.scrollT
var sLeft = document.documentElement.scrollLeft || document.body.scrollL
var winSize = function () {
var xScroll, yScroll, windowWidth, windowHeight, pageWidth, pageH
// innerHeight获取的是可视窗口的高度,IE不支持此属性
if (window.innerHeight && window.scrollMaxY) {
xScroll = document.body.scrollW
yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight & document.body.offsetHeight) { // all but Explorer Mac
xScroll = document.body.scrollW
yScroll = document.body.scrollH
} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
xScroll = document.body.offsetW
yScroll = document.body.offsetH
if (self.innerHeight) {
// all except Explorer
windowWidth = self.innerW
windowHeight = self.innerH
} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
windowWidth = document.documentElement.clientW
windowHeight = document.documentElement.clientH
} else if (document.body) { // other Explorers
windowWidth = document.body.clientW
windowHeight = document.body.clientH
// for small pages with total height less then height of the viewport
if (yScroll & windowHeight) {
pageHeight = windowH
pageHeight = yS
// for small pages with total width less then width of the viewport
if (xScroll & windowWidth) {
pageWidth = windowW
pageWidth = xS
'pageWidth': pageWidth,
'pageHeight': pageHeight,
'windowWidth': windowWidth,
'windowHeight': windowHeight
//alert(winSize.pageWidth);
var styleStr = 'top:0;left:0;position:z-index:10000;background:#666;width:' + winSize.pageWidth + 'height:' + (winSize.pageHeight + 30) + '';
styleStr += (isIe) ? "filter:alpha(opacity=80);" : "opacity:0.8;"; //遮罩层DIV
var shadowDiv = document.createElement('div'); //添加阴影DIV
shadowDiv.style.cssText = styleS //添加样式
shadowDiv.id = "shadowDiv";
//如果是IE6则创建IFRAME遮罩SELECT
if (isIE6) {
var maskIframe = document.createElement('iframe');
maskIframe.style.cssText = 'width:' + winSize.pageWidth + 'height:' + (winSize.pageHeight + 30) + 'position:visibility:z-index:-1;filter:alpha(opacity=0);';
maskIframe.frameborder = 0;
maskIframe.src = "about:blank";
shadowDiv.appendChild(maskIframe);
document.body.insertBefore(shadowDiv, document.body.firstChild); //遮罩层加入文档
var styleStr1 = 'display:position:_position:left:' + (winSize.windowWidth / 2 - 125) + 'top:' + (winSize.windowHeight / 2 - 70) + '_top:' + (winSize.windowHeight / 2 + top - 150) + ''; //弹出框的位置
var alertBox = document.createElement('div');
var alertTitle = document.createElement('div');
alertTitle.id = 'alertTitle';
alertBox.id = 'alertMsg';
alertBox.style.cssText = styleStr1;
//创建弹出框里面的内容P标签
var alertMsg_info = document.createElement('P');
alertMsg_info.id = 'alertMsg_info';
alertMsg_info.innerHTML =
alertTitle.innerHTML = '提示信息!';
alertBox.appendChild(alertTitle);
alertBox.appendChild(alertMsg_info);
//创建按钮
var styleStr2 = 'display:position:_position:left:' + (winSize.windowWidth / 2 - 125) + 'top:' + (winSize.windowHeight / 2 + 40) + '_top:' + (winSize.windowHeight / 2 + top - 150) + ''; //弹出框的位置
var alertBottom = document.createElement('div');
alertBottom.id = 'alertBottom';
alertBottom.style.cssText = styleStr2;
if (mode === 1) {
var btn1 = document.createElement('a');
btn1.id = 'alertMsg_btn1';
btn1.href = 'javas' + 'cript:void(0)';
btn1.innerHTML = '&cite&确定&/cite&';
btn1.onclick = function () {
if (typeof callback == 'function') callback(this);////传递了回调则执行回调,将按钮作为回调参数
document.body.removeChild(alertBox);
document.body.removeChild(shadowDiv);
document.body.removeChild(alertBottom);
//document.getElementById("logout").click();
alertBottom.appendChild(btn1);
var btn2 = document.createElement('a');
btn2.id = 'alertMsg_btn2';
btn2.href = 'javas' + 'cript:void(0)';
btn2.innerHTML = '&cite&取消&/cite&';
btn2.onclick = function () {
if (typeof callback == 'function') callback(this); ////传递了回调则执行回调,将按钮作为回调参数
document.body.removeChild(alertBox);
document.body.removeChild(shadowDiv);
document.body.removeChild(alertBottom);
var btn2 = document.createElement('a');
btn2.id = 'alertMsg_btn2';
btn2.href = 'javas' + 'cript:void(0)';
btn2.innerHTML = '&cite&确定&/cite&';
btn2.onclick = function () {
if (typeof callback == 'function') callback(this); ////传递了回调则执行回调,将按钮作为回调参数
document.body.removeChild(alertBox);
document.body.removeChild(shadowDiv);
document.body.removeChild(alertBottom);
alertBottom.appendChild(btn2);
document.body.appendChild(alertBox);
document.body.appendChild(alertBottom);
function confirmMsg() {
alertMsg("测试", 1, function (btn) { //将点击的按钮作为回调函数的参数
var text = btn.innerHTML.replace(/&[^&]+&/g, ''); //去掉所有html标签
alert('点击了“' + text + '”按钮');
&input type="button" value="Test the alert"
onclick="confirmMsg('test confirm');"&
其他相似问题

我要回帖

更多关于 js函数返回true false 的文章

 

随机推荐