當用戶從引導程式關閉此警報時如何存盤 cookie
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>Holy guacamole!</strong> You should check in on some of those fields below.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
我知道如何使用 "onclick" 來做到這一點,但是有沒有不修改上述代碼的另一種方法?
uj5u.com熱心網友回復:
使用警報事件之一觸發您的代碼
| 事件 | 描述 |
|---|---|
close.bs.alert |
close呼叫實體方法時會立即觸發此事件。 |
closed.bs.alert |
當警報關閉時觸發此事件(將等待 CSS 轉換完成)??。 |
$(".alert").on("closed.bs.alert", (e) => {
console.log("here's where I would set a cookie")
// doesn't work in StackSnippet sandbox
// document.cookie = "favorite_framework=Bootstrap; SameSite=None; Secure";
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA 058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5 76PVCmYl" crossorigin="anonymous"></script>
<div class="container">
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>Holy guacamole!</strong> You should check in on some of those fields below.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
</div>
uj5u.com熱心網友回復:
那么您是在問如何在不修改元素的情況下將偵聽器附加到 dom 元素嗎?
<script>
$('.alert-dismissible button.btn-close').click(() => {
// cookie stuff here.
});
</script>
您可以針對它們,以上是使用 jquery。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/368862.html
標籤:javascript 查询 推特引导
