如何使提示框输入调用函数?

时间:2015-03-07 18:27:27

标签: javascript function if-statement prompt

如何摆脱按钮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>

1 个答案:

答案 0 :(得分:1)

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

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

<body onload="onLoaded()">

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