当我单击按钮时,该消息显示在右上角。只有经过一两次尝试,它才会位于顶部中心。但是,在提交表单时,它应该位于首次点击本身的顶部中心。谁能告诉我为什么会这样发生以及如何纠正它?我在下面包含了代码段。
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<link rel="stylesheet"
href="http://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css">
</head>
<body>
<p>toast-top-center</p>
<button onclick="myFunction()">Click Here</button>
<p id="demo"></p>
<script>
function myFunction() {
Command: toastr["error"]
("I'm not in the top-center!")
toastr.options = {
"closeButton" : false,
"debug" : false,
"newestOnTop" : false,
"progressBar" : false,
"positionClass" : "toast-top-center",
"preventDuplicates" : false,
"onclick" : null,
"showDuration" : "300",
"hideDuration" : "1000",
"timeOut" : "5000",
"extendedTimeOut" : "1000",
"showEasing" : "swing",
"hideEasing" : "linear",
"showMethod" : "fadeIn",
"hideMethod" : "fadeOut"
}
}
</script>
</body>
</html>
最佳答案
我认为错误是因为您在阅读选项之前先举杯了。
这就是为什么第一次烤面包在右上方。
在功能结束时运行吐司,它将起作用。
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<link rel="stylesheet"
href="http://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css">
</head>
<body>
<p>toast-top-center</p>
<button onclick="myFunction()">Click Here</button>
<p id="demo"></p>
<script>
function myFunction() {
toastr.options = {
"positionClass" : "toast-top-center",
"closeButton" : false,
"debug" : false,
"newestOnTop" : false,
"progressBar" : false,
"preventDuplicates" : false,
"onclick" : null,
"showDuration" : "300",
"hideDuration" : "1000",
"timeOut" : "5000",
"extendedTimeOut" : "1000",
"showEasing" : "swing",
"hideEasing" : "linear",
"showMethod" : "fadeIn",
"hideMethod" : "fadeOut"
}
Command: toastr["success"]
("I'm in the top-center!")
}
</script>
</body>
</html>
关于javascript - Toastr:第一次单击按钮时未应用toast-top-center,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38502766/