gpt4 book ai didi

javascript - 如何在延迟后显示模态?

转载 作者:行者123 更新时间:2023-12-03 00:31:27 25 4
gpt4 key购买 nike

我希望如果用户在我的网站上停留大约 20 秒,就会显示一个询问用户姓名和电子邮件地址的模式。我已获取模态 div 的 id 并将其存储在函数 myFunction() 中。这里的主要问题是 20 秒后没有任何显示。

以下是我的代码:-

<!-- Apart from window.onload, I have also mentioned the function with body onload-->

<body onload="myFunction" id="myPage" data-spy="scroll" data-target=".navbar" data-offset="60">

<script type="text/javascript">

window.setTimeout(function myFunction() {

document.getElementById("exampleModal");

},20000);

</script>

<!-- Modal code -->

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">New message</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form action="MessageServlet" method="post">
<div class="form-group">
<label for="recipient-name" class="col-form-label">Recipient:</label>
<input type="text" class="form-control" id="recipient-name" value="svchaturvedi9@gmail.com" readonly="readonly">
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Your Email ID:</label>
<input type="text" class="form-control" id="email-name" name="email-name" placeholder="Email ID">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Message:</label>
<textarea class="form-control" id="message-text" name="message-text"></textarea>
</div>


<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Send message</button>
</form>
</div>
</div>

</div>

</div>

我的目标是,当用户在模式字段中输入值并单击“确定”时,用户的数据将发送到电子邮件 servlet。谢谢

最佳答案

您可以看到下面的示例。根据您的需要替换第二个和模式代码。

setTimeout(function() {
$('#myModal').modal('toggle');
}, 200);
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>

</div>
</div>

关于javascript - 如何在延迟后显示模态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53849407/

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