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

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

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

问题

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

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Page Template</title>

    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">

    <!-- Font Awesome CSS -->
    <link rel="stylesheet" href="/assets/fontawesome-5.15.1/css/all.min.css" />

    <!-- Bootstrap JavaScript -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>

<body>
    <h1>Welcome to the Page</h1>

    <!-- Your content here -->
    <div id="toast-sticky-message" class="position-fixed bottom-0 end-0 p-3" style="z-index: 1500000">
        <div class="toast">
            <div class="toast-body">
                <span id="toast-content"></span>
                <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
            </div>
        </div>
    </div>

    <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

            makeAlertElem('hello world');
        });
    </script>
</body>

</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 -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Page Template&lt;/title&gt;
&lt;!-- jQuery --&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.3.1.min.js&quot;&gt;&lt;/script&gt;
&lt;!-- Bootstrap CSS --&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot;&gt;
&lt;!-- Font Awesome CSS --&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;/assets/fontawesome-5.15.1/css/all.min.css&quot; /&gt;
&lt;!-- Bootstrap JavaScript --&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Welcome to the Page&lt;/h1&gt;
&lt;!-- Your content here --&gt;
&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;
&lt;div class=&quot;toast&quot;&gt;
&lt;div class=&quot;toast-body&quot;&gt;
&lt;span id=&quot;toast-content&quot;&gt;&lt;/span&gt;
&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;
&lt;/div&gt;
&lt;/div&gt;
&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

        makeAlertElem(&#39;hello world&#39;);
});

</script>
</body>

&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 2

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

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

makeAlertElem('hello world');
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>

<h1>Welcome to the Page</h1>

<div id="toast-sticky-message" class="position-fixed bottom-0 end-0 p-3" style="z-index: 1500000">
  <div class="toast">
    <div class="toast-body">
      <span id="toast-content"></span>
      <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
    </div>
  </div>
</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 -->

function makeAlertElem(string, id = false, replace = false, color = &quot;alert-info&quot;, timeout = 5000,
dismissable = true) {
let toastElement = document.querySelector(&quot;#toast-sticky-message .toast&quot;);
if (!toastElement) {
return false;
}
toastElement.querySelector(&quot;#toast-content&quot;).innerHTML = string;
let toast = new bootstrap.Toast(toastElement);
toast.show();
}
// end function makeAlertElem
makeAlertElem(&#39;hello world&#39;);

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

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot;&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
&lt;h1&gt;Welcome to the Page&lt;/h1&gt;
&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;
&lt;div class=&quot;toast&quot;&gt;
&lt;div class=&quot;toast-body&quot;&gt;
&lt;span id=&quot;toast-content&quot;&gt;&lt;/span&gt;
&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;
&lt;/div&gt;
&lt;/div&gt;
&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:

确定