the toast is not showing in bootstrap 5 even though no error in developer tools

huangapple go评论105阅读模式
英文:

the toast is not showing in bootstrap 5 even though no error in developer tools

问题

为什么下面的代码中 Toast 不显示?我正在使用 bootstrap 5。我完全不知道为什么,因为在开发者工具中没有任何错误。任何帮助都会感激。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Page Template</title>
  7. <!-- jQuery -->
  8. <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  9. <!-- Bootstrap CSS -->
  10. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
  11. <!-- Font Awesome CSS -->
  12. <link rel="stylesheet" href="/assets/fontawesome-5.15.1/css/all.min.css" />
  13. <!-- Bootstrap JavaScript -->
  14. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
  15. </head>
  16. <body>
  17. <h1>Welcome to the Page</h1>
  18. <!-- Your content here -->
  19. <div id="toast-sticky-message" class="position-fixed bottom-0 end-0 p-3" style="z-index: 1500000">
  20. <div class="toast">
  21. <div class="toast-body">
  22. <span id="toast-content"></span>
  23. <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
  24. </div>
  25. </div>
  26. </div>
  27. <script>
  28. $(function() {
  29. function makeAlertElem(string, id = false, replace = false, color = "alert-info", timeout = 5000,
  30. dismissable = true) {
  31. let toastElement = document.getElementById("toast-sticky-message");
  32. if (!toastElement) {
  33. return false;
  34. }
  35. console.log("toast string", string);
  36. toastElement.querySelector("#toast-content").innerHTML = string;
  37. let toast = new bootstrap.Toast(toastElement);
  38. console.log("toast 2", toast);
  39. toast.show();
  40. }
  41. // end function makeAlertElem
  42. makeAlertElem('hello world');
  43. });
  44. </script>
  45. </body>
  46. </html>
英文:

why is the Toast not showing in the code below? I'm using bootstrap 5. I haven't the slightest idea why because there isn't any errors in the developer's tools. any help is appreciated.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  6. &lt;title&gt;Page Template&lt;/title&gt;
  7. &lt;!-- jQuery --&gt;
  8. &lt;script src=&quot;https://code.jquery.com/jquery-3.3.1.min.js&quot;&gt;&lt;/script&gt;
  9. &lt;!-- Bootstrap CSS --&gt;
  10. &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot;&gt;
  11. &lt;!-- Font Awesome CSS --&gt;
  12. &lt;link rel=&quot;stylesheet&quot; href=&quot;/assets/fontawesome-5.15.1/css/all.min.css&quot; /&gt;
  13. &lt;!-- Bootstrap JavaScript --&gt;
  14. &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
  15. &lt;/head&gt;
  16. &lt;body&gt;
  17. &lt;h1&gt;Welcome to the Page&lt;/h1&gt;
  18. &lt;!-- Your content here --&gt;
  19. &lt;div id=&quot;toast-sticky-message&quot; class=&quot;position-fixed bottom-0 end-0 p-3&quot; style=&quot;z-index: 1500000&quot;&gt;
  20. &lt;div class=&quot;toast&quot;&gt;
  21. &lt;div class=&quot;toast-body&quot;&gt;
  22. &lt;span id=&quot;toast-content&quot;&gt;&lt;/span&gt;
  23. &lt;button type=&quot;button&quot; class=&quot;btn-close&quot; data-bs-dismiss=&quot;toast&quot; aria-label=&quot;Close&quot;&gt;&lt;/button&gt;
  24. &lt;/div&gt;
  25. &lt;/div&gt;
  26. &lt;/div&gt;

<script>
$(function() {
function makeAlertElem(string, id = false, replace = false, color = "alert-info", timeout = 5000,
dismissable = true) {
let toastElement = document.getElementById("toast-sticky-message");
if (!toastElement) {
return false;
}
console.log("toast string", string);
toastElement.querySelector("#toast-content").innerHTML = string;
let toast = new bootstrap.Toast(toastElement);
console.log("toast 2", toast);
toast.show();
}
// end function makeAlertElem

  1. makeAlertElem(&#39;hello world&#39;);
  2. });

</script>
</body>

  1. &lt;/html&gt;

<!-- end snippet -->

答案1

得分: 2

这是因为 Toast 使用了错误的元素进行初始化。你应该目标定位具有类名 toast 的元素:

  1. function makeAlertElem(string, id = false, replace = false, color = "alert-info", timeout = 5000, dismissable = true) {
  2. let toastElement = document.querySelector("#toast-sticky-message .toast");
  3. if (!toastElement) {
  4. return false;
  5. }
  6. toastElement.querySelector("#toast-content").innerHTML = string;
  7. let toast = new bootstrap.Toast(toastElement);
  8. toast.show();
  9. }
  10. // end function makeAlertElem
  11. makeAlertElem('hello world');
  1. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
  2. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
  3. <h1>Welcome to the Page</h1>
  4. <div id="toast-sticky-message" class="position-fixed bottom-0 end-0 p-3" style="z-index: 1500000">
  5. <div class="toast">
  6. <div class="toast-body">
  7. <span id="toast-content"></span>
  8. <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
  9. </div>
  10. </div>
  11. </div>

注意:我只翻译了代码部分,不包括注释。

英文:

It's because the Toast is initialized with the wrong element. You should target the element with class toast :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. function makeAlertElem(string, id = false, replace = false, color = &quot;alert-info&quot;, timeout = 5000,
  2. dismissable = true) {
  3. let toastElement = document.querySelector(&quot;#toast-sticky-message .toast&quot;);
  4. if (!toastElement) {
  5. return false;
  6. }
  7. toastElement.querySelector(&quot;#toast-content&quot;).innerHTML = string;
  8. let toast = new bootstrap.Toast(toastElement);
  9. toast.show();
  10. }
  11. // end function makeAlertElem
  12. makeAlertElem(&#39;hello world&#39;);

<!-- language: lang-html -->

  1. &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot;&gt;
  2. &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
  3. &lt;h1&gt;Welcome to the Page&lt;/h1&gt;
  4. &lt;div id=&quot;toast-sticky-message&quot; class=&quot;position-fixed bottom-0 end-0 p-3&quot; style=&quot;z-index: 1500000&quot;&gt;
  5. &lt;div class=&quot;toast&quot;&gt;
  6. &lt;div class=&quot;toast-body&quot;&gt;
  7. &lt;span id=&quot;toast-content&quot;&gt;&lt;/span&gt;
  8. &lt;button type=&quot;button&quot; class=&quot;btn-close&quot; data-bs-dismiss=&quot;toast&quot; aria-label=&quot;Close&quot;&gt;&lt;/button&gt;
  9. &lt;/div&gt;
  10. &lt;/div&gt;
  11. &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月22日 17:06:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定