gpt4 book ai didi

javascript - 如何让提示框输入调用函数?

转载 作者:行者123 更新时间:2023-11-28 00:31:44 26 4
gpt4 key购买 nike

如何摆脱按钮 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”中的代码(没有提示)?在这种情况下,您需要在页面加载后运行提示。您可以在此处查看示例: http://jsfiddle.net/7a6e9r2f/1/

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

<body onload="onLoaded()">

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

关于javascript - 如何让提示框输入调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28918435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com