javascript-javaScript中返回true和false的问题,求大神解答!!

问题描述

javaScript中返回true和false的问题,求大神解答!!
 <%--
  Created by IntelliJ IDEA.
  User: TopbeCoder5
  Date: 2015/1/18
  Time: 20:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <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.scrollTop;
            var isIe = (document.all) ? true : false;
            var isIE6 = isIe && !window.XMLHttpRequest;
            var sTop = document.documentElement.scrollTop || document.body.scrollTop;
            var sLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
            var winSize = function () {
                var xScroll, yScroll, windowWidth, windowHeight, pageWidth, pageHeight;
                // innerHeight获取的是可视窗口的高度,IE不支持此属性
                if (window.innerHeight && window.scrollMaxY) {
                    xScroll = document.body.scrollWidth;
                    yScroll = window.innerHeight + window.scrollMaxY;
                } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
                    xScroll = document.body.scrollWidth;
                    yScroll = document.body.scrollHeight;
                } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
                    xScroll = document.body.offsetWidth;
                    yScroll = document.body.offsetHeight;
                }

                if (self.innerHeight) {    // all except Explorer
                    windowWidth = self.innerWidth;
                    windowHeight = self.innerHeight;
                } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
                    windowWidth = document.documentElement.clientWidth;
                    windowHeight = document.documentElement.clientHeight;
                } else if (document.body) { // other Explorers
                    windowWidth = document.body.clientWidth;
                    windowHeight = document.body.clientHeight;
                }

                // for small pages with total height less then height of the viewport
                if (yScroll < windowHeight) {
                    pageHeight = windowHeight;
                } else {
                    pageHeight = yScroll;
                }

                // for small pages with total width less then width of the viewport
                if (xScroll < windowWidth) {
                    pageWidth = windowWidth;
                } else {
                    pageWidth = xScroll;
                }

                return {
                    'pageWidth': pageWidth,
                    'pageHeight': pageHeight,
                    'windowWidth': windowWidth,
                    'windowHeight': windowHeight
                }
            }();
            //alert(winSize.pageWidth);
            //遮罩层
            var styleStr = 'top:0;left:0;position:absolute;z-index:10000;background:#666;width:' + winSize.pageWidth + 'px;height:' + (winSize.pageHeight + 30) + 'px;';
            styleStr += (isIe) ? "filter:alpha(opacity=80);" : "opacity:0.8;"; //遮罩层DIV
            var shadowDiv = document.createElement('div'); //添加阴影DIV
            shadowDiv.style.cssText = styleStr; //添加样式
            shadowDiv.id = "shadowDiv";
            //如果是IE6则创建IFRAME遮罩SELECT
            if (isIE6) {
                var maskIframe = document.createElement('iframe');
                maskIframe.style.cssText = 'width:' + winSize.pageWidth + 'px;height:' + (winSize.pageHeight + 30) + 'px;position:absolute;visibility:inherit;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:block;position:fixed;_position:absolute;left:' + (winSize.windowWidth / 2 - 125) + 'px;top:' + (winSize.windowHeight / 2 - 70) + 'px;_top:' + (winSize.windowHeight / 2 + top - 150) + 'px;'; //弹出框的位置
            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 = msg;
            alertTitle.innerHTML = '提示信息!';
            alertBox.appendChild(alertTitle);
            alertBox.appendChild(alertMsg_info);
            //创建按钮
            var styleStr2 = 'display:block;position:fixed;_position:absolute;left:' + (winSize.windowWidth / 2 - 125) + 'px;top:' + (winSize.windowHeight / 2 + 40) + 'px;_top:' + (winSize.windowHeight / 2 + top - 150) + 'px;'; //弹出框的位置
            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();
                    return true;
                };
                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);
                    return false;
                };
            } else {
                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);
                    return false;
                };
            }
            alertBottom.appendChild(btn2);
            document.body.appendChild(alertBox);
            document.body.appendChild(alertBottom);

        }
        function confirmMsg() {
            if (alertMsg("测试", 1)) {
                alert("点击了确定");
            } else {
                alert("点击了取消");
            }
        }
    </script>
</head>
<body>

<input type="button" value="Test the alert"
       onclick="return confirmMsg('test confirm');">

</body>
</html>

为什么一点botton就直接运行了false啊??

解决方案

alertMsg没有return,默认返回的undefined,对于if来说是false

解决方案二:

目测,alertMsg是异步执行的,你需要将它的onclick暴露出来,把你的确定取消的判断写在onclick里面。

解决方案三:

直接断点调试一下

解决方案四:

你这个是层模拟的,和系统的alert不一样,无法挂起js代码的执行。需要回掉函数,再点击你的按钮时执行回掉函数做需要的操作

解决方案五:

帮你大概改了下。

 <html>
<head><meta http-equiv="content-type" content="text/html;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.scrollTop;
            var isIe = (document.all) ? true : false;
            var isIE6 = isIe && !window.XMLHttpRequest;
            var sTop = document.documentElement.scrollTop || document.body.scrollTop;
            var sLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
            var winSize = function () {
                var xScroll, yScroll, windowWidth, windowHeight, pageWidth, pageHeight;
                // innerHeight获取的是可视窗口的高度,IE不支持此属性
                if (window.innerHeight && window.scrollMaxY) {
                    xScroll = document.body.scrollWidth;
                    yScroll = window.innerHeight + window.scrollMaxY;
                } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
                    xScroll = document.body.scrollWidth;
                    yScroll = document.body.scrollHeight;
                } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
                    xScroll = document.body.offsetWidth;
                    yScroll = document.body.offsetHeight;
                }

                if (self.innerHeight) {    // all except Explorer
                    windowWidth = self.innerWidth;
                    windowHeight = self.innerHeight;
                } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
                    windowWidth = document.documentElement.clientWidth;
                    windowHeight = document.documentElement.clientHeight;
                } else if (document.body) { // other Explorers
                    windowWidth = document.body.clientWidth;
                    windowHeight = document.body.clientHeight;
                }

                // for small pages with total height less then height of the viewport
                if (yScroll < windowHeight) {
                    pageHeight = windowHeight;
                } else {
                    pageHeight = yScroll;
                }

                // for small pages with total width less then width of the viewport
                if (xScroll < windowWidth) {
                    pageWidth = windowWidth;
                } else {
                    pageWidth = xScroll;
                }

                return {
                    'pageWidth': pageWidth,
                    'pageHeight': pageHeight,
                    'windowWidth': windowWidth,
                    'windowHeight': windowHeight
                }
            } ();
            //alert(winSize.pageWidth);
            //遮罩层
            var styleStr = 'top:0;left:0;position:absolute;z-index:10000;background:#666;width:' + winSize.pageWidth + 'px;height:' + (winSize.pageHeight + 30) + 'px;';
            styleStr += (isIe) ? "filter:alpha(opacity=80);" : "opacity:0.8;"; //遮罩层DIV
            var shadowDiv = document.createElement('div'); //添加阴影DIV
            shadowDiv.style.cssText = styleStr; //添加样式
            shadowDiv.id = "shadowDiv";
            //如果是IE6则创建IFRAME遮罩SELECT
            if (isIE6) {
                var maskIframe = document.createElement('iframe');
                maskIframe.style.cssText = 'width:' + winSize.pageWidth + 'px;height:' + (winSize.pageHeight + 30) + 'px;position:absolute;visibility:inherit;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:block;position:fixed;_position:absolute;left:' + (winSize.windowWidth / 2 - 125) + 'px;top:' + (winSize.windowHeight / 2 - 70) + 'px;_top:' + (winSize.windowHeight / 2 + top - 150) + 'px;'; //弹出框的位置
            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 = msg;
            alertTitle.innerHTML = '提示信息!';
            alertBox.appendChild(alertTitle);
            alertBox.appendChild(alertMsg_info);
            //创建按钮
            var styleStr2 = 'display:block;position:fixed;_position:absolute;left:' + (winSize.windowWidth / 2 - 125) + 'px;top:' + (winSize.windowHeight / 2 + 40) + 'px;_top:' + (winSize.windowHeight / 2 + top - 150) + 'px;'; //弹出框的位置
            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();
                    return true;
                };
                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);
                    return false;
                };
            } else {
                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);
                    return false;
                };
            }
            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 + '”按钮');
            });
        }
    </script>
</head>
<body>

<input type="button" value="Test the alert"
       onclick="confirmMsg('test confirm');">

</body>
</html>
时间: 2024-08-08 14:30:12

javascript-javaScript中返回true和false的问题,求大神解答!!的相关文章

java容器 collection-下面这个小程序打印出的为什么是true和false啊,求大神分析一下,万分感谢~

问题描述 下面这个小程序打印出的为什么是true和false啊,求大神分析一下,万分感谢~ public class BasicContainer { public static void main(String[] args) { Collection c = new HashSet(); c.add("hello"); c.add(new Name("lu","dongdong")); c.add(new Integer(100)); Sys

数据传输-WriteFile失败,GetLastError返回1,功能错误,求大神解答啊

问题描述 WriteFile失败,GetLastError返回1,功能错误,求大神解答啊 向DSP写入数据,设备打开成功句柄有效,WriteFile失败,GetLastError返回1 部分代码: DWORD dwTransRet = 0, dwRecRet = 0; BOOL bTransRet, bRecRet; DWORD dwError; unsigned char RecBuff[BufferSize] = { 0 }; unsigned char TransBuff[BufferSi

php编程-在mysqli-&amp;amp;gt;query()语句中遇到的两个问题,求大神解答下

问题描述 在mysqli->query()语句中遇到的两个问题,求大神解答下 <?php $mysqli=new mysqli("localhost", "root", "", "test"); if($mysqli->connect_error){ die( "connection failed".$mysqli->connect_error); }else{ echo "

ADO向服务器发送请求返回DataSet为什么没有值,求大神解答。

问题描述 我自己向服务器更数据可以,但是要返回个DataSet需要显示到dataGridView...也不报错只是没有数据.在服务器的数据库里有数据是需要序列化吗??? 解决方案 解决方案二:datagridview.databind()这句也要执行.不然就是绑定了数据也不会显示解决方案三:你好为什么datagridview.databind()找不到这句呢解决方案四:你的dta.Table[0]有数据吗?解决方案五:有数据的解决方案六:引用4楼qq_31924339的回复: 有数据的 强制刷新

A窗体通过串口读入数据,解析后把结果传给B窗体TextBox中,在A窗体中通过按钮把B窗体显示在A窗体的Panel中,程序怎么改啊,求大神解答

问题描述 解决方案 解决方案二:有必要搞两个窗体搞来搞去的吗?解决方案三:你接下来需要学习的那个概念叫做"用户控件",而不是什么"窗口".

代码-android中发送验证码,才开始学的,求大神解答。

问题描述 android中发送验证码,才开始学的,求大神解答. 这个现在还没有数据库,只是我学习用的,点发送验证码,后面会提示多少秒后重新获取验证码,求大神给段代码. 解决方案 MyCoun myCoun = new MyCoun(60000 1000); /** * 倒计时定时器 * * @author 1 * */ public class MyCoun extends CountDownTimer { public MyCoun(long millisInFuture long count

java-Java中多线程同步锁报无效指针异常是何原因,求大神解答!

问题描述 Java中多线程同步锁报无效指针异常是何原因,求大神解答! /* 需求: 使用lock接口实现商品的生产和销售的动态平衡 思路: 1.创建被共同操作的数据类Resource,并通过多态引用分别创建lock和condition子类对象以及资源的获取和赋值方法 2.创建两个相反操作即生产和销售的类同时实现Runnable接口,并覆盖run方法 3.在主函数中,创建资源对象,并作为共同参数分别传递给生产和销售两个线程实现类的构造函数 4.分别将生产和销售类的对象作为参数,传给两个线程,并各自

求大神解答一下-java中对象流objectstream问题

问题描述 java中对象流objectstream问题 输出的为什么不是cyh男20 ym女20求大神解答!!!!!!!!!! 解决方案 你的代码和我这个一样吗?麻烦把你的代码粘全了,我看看 解决方案二: 这个是照片......... 解决方案三: 我和你写的差不多,不知道你为啥会这样,我给你粘出我的代码package lianxi; import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOE

关于oracle中存储过程的。在线等,求大神们解答

问题描述 关于oracle中存储过程的.在线等,求大神们解答 select hmv.hy_time_stamp mm.cu_name mm.meter_numberfrom (select hc.hy_name_last as cu_namehm.hy_meter_number as meter_number from hy_customer hchy_metainfo_customer hmc hy_meter_point hmp hy_metainfo_meter hmm hy_meter