【发布时间】:2014-08-15 06:51:01
【问题描述】:
我为 Javascript 制作了一个自定义提示框,因此我不必使用现有的提示框,但是我无法弄清楚如何直接从 cPrompt() 函数返回值。我希望能够像普通提示一样使用它,并且能够执行 var name = cPrompt("Whats your name") 之类的操作,但是要关闭自定义提示,我必须在单击按钮时使用一个事件来触发一个名为 ok() 的函数。这使我无法直接从 cPrompt 函数返回值。谁能弄清楚我该怎么做?这是我的代码:
HTML:
<html>
<head>
<title>Custom Prompt</title>
</head>
<body>
<p>content</p>
<button onclick="cPrompt('Message goes here')">Click me</button>
<div id="overlay"></div>
<div id="pBox">
<div id="cPromptOut" onclick="ok()"></div>
<input type="text" id="cPromptIn"/>
</div>
<link rel='stylesheet' href='customPrompt.css'/>
<script src='customPrompt.js'></script>
</body>
</html>
CSS:
#overlay{
width:100%;
height:100%;
position:fixed;
background-color:grey;
opacity:0.5;
z-index: 10;
display: none;
left:0;
top:0;
}
#pBox{
width:50%;
height:30%;
position:fixed;
background-color:red;
left:25%;
top:20%;
z-index: 11;
display:none;
}
#cPromptOut{
width:100%;
height:50%;
background-color: green;
z-index: 12;
text-align: center;
font-size: 1.5em
}
#cPromptIn{
width:100%;
height:50%;
border:1px solid black;
text-align: center;
font-size: 1.5em
}
JS:
var overlay = document.getElementById("overlay")
var pBox = document.getElementById("pBox")
var cPromptOut = document.getElementById("cPromptOut")
var In = document.getElementById("cPromptIn").value
function cPrompt(msg){
overlay.style.display = "block";
pBox.style.display = "block";
cPromptOut.innerHTML = msg;
}
function ok(){
overlay.style.display = "none";
pBox.style.display = "none";
}
console.log(cPrompt("enter you name"))
所以现在我无法收集框中的值。谁能弄清楚我该怎么做?请记住,我希望能够像提示一样调用它,而不必使用任何其他调用,例如 console.log(cPrompt("say something"))。我可能试图过度思考这个或者可能是不可能的,欢迎任何想法。
【问题讨论】:
标签: javascript return dom-events custom-controls prompt