【问题标题】:How to make a prompt box input invoke a function?如何让提示框输入调用函数?
【发布时间】:2015-05-09 05:13:46
【问题描述】:

如何去掉 onclick="clickMe()" 按钮,让提示框中的输入执行功能?
我想我需要提示框中的输入来调用 if 语句?

<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<script>
   function clickMe(){
var valueInput=prompt("Please input weekday, weekend?");
var weekdayElement = document.getElementById("weekday"); 
var weekendElement = document.getElementById("weekend"); 
    if (valueInput=="weekday"){
weekdayElement.style.height = "200px"; 
weekdayElement.style.width = "250px"; 
weekdayElement.style.fontSize = "15px"; 
weekdayElement.style.color="blue";  
    if (valueInput=="weekend"){
weekendElement.style.height = "200px"; 
weekendElement.style.width = "250px"; 
weekendElement.style.fontSize = "15px"; 
weekendElement.style.color="blue";  
}
</script>
</head>
<body>
<h1>This is an example on DOM</h1>
<div id="weekday">
    weekdays
    <p>Monday</p>
    <p>Tuesday</p>
    <p>Wednesday</p>
    <p>Thursday</p>
    <p>Friday</p>
</div>
<div id="weekend">
    weekends
    <p>Saturday</p>
    <p>Sunday</p>
</div> 
<p><button onclick="clickMe()">Try it</button></p>
</body>
</html>

【问题讨论】:

  • 你的问题没有意义。 clickMe 触发提示框,但您问的是如何使提示框触发clickMe。这就像要求提示框及时返回并触发自身。您可能需要重新考虑您的问题并尝试更清楚地陈述它。
  • 你能再澄清一点吗?目前,此代码设置为:(1)单击按钮,(2)clickMe() 函数运行,并显示提示。 (2) 收集输入后,运行剩下的代码。您希望提示自动运行,还是没有看到规定的行为?

标签: javascript function if-statement prompt


【解决方案1】:

如果我理解正确,您希望在页面加载时提示跳转,然后在用户输入值后执行“clickMe”中的代码(没有提示)? 在这种情况下,您需要在页面加载后运行提示。你可以在这里看到一个例子: http://jsfiddle.net/7a6e9r2f/1/

请注意,jsfiddle 会在页面加载完毕后自动调用底部的 javascript 代码。在您的情况下,您需要添加

<body onload="onLoaded()">

到您的 html 以便调用 onLoaded 函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    相关资源
    最近更新 更多