单击框时弹出使用js

时间:2014-10-31 17:23:36

标签: javascript html css

我创建了一个简单的网站,它将成为更大项目的一部分:
这是屏幕截图:
Website
这是代码:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
    function popup () {
        var thought = prompt("Please enter your thougt");
    }
</script>
<style>
.fixed-size-square {
    display: table;
    width: 200px;
    height: 200px;
    background: green;
}
.fixed-size-square span {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
    color: white
}
.size-square {
    display: table;
    width: 200px;
    height: 200px;
    background: green;
}
.size-square span {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
    color: white
}
.square {
    display: table;
    width: 200px;
    height: 200px;
    background: green;
}
.square span {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
    color: white
}
.fixed-size-square, .size-square, .square {
    float: left;
    margin: 20px;
    zoom: 1; 
    position: relative;
    box-shadow: 10px 10px 5px #888888;
    /*position: absolute;*/
    /*margin: auto;*/
}
button {
        position: relative;
        margin: 0px auto;
}
.divn {  
    padding:30px; 
    background:red; 
    width:1px; 
    border-radius:35px; 
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    /*position: absolute;
    margin: auto;*/
} 
</style>
</head>
<body>
<h1>Welcome to zen module</h1>
<div class='divn'></div> 
<div class="fixed-size-square" onclick="popup();">
  <span>Past Thoughts</span>
</div>

<div class="size-square" onclick="popup();">
    <span>Present Thoughts</span>
</div>

<div class="square" onclick="popup();">
    <span>Future Thoughts</span>
</div>
<button>I've no thoughts</button>
</body>
</html>

我想为window.prompt("question", "default")提供2个功能。当我在提示符中单击Okay时,函数应显示为Okay clicked,单击cancel按钮时,另一个函数应显示为cancel clicked。我该怎么做?

3 个答案:

答案 0 :(得分:1)

尝试window.prompt(“问题”,“默认”)

这是一个演示:

http://www.w3schools.com/js/tryit.asp?filename=tryjs_prompt

答案 1 :(得分:1)

使用以下代码

    var x = prompt("dvdvbd", "default");
if (x != null) {  //if user click ok 
    //do this
}
else {  //if user click cancel
    //do this
}

答案 2 :(得分:1)

如果用户点击取消,则提示框返回null,如果他点击OK(没有输入任何内容),则返回空字符串。您需要做的就是检查prompt()返回null

if (prompt("Question") != null){
  
  alert("You clicked OK!");
  
  }else {alert("What\'s wrong with you? Click OK!")}