我在用htmx https://htmx.org/向我的页面添加一些 AJAX 调用。我有一个cart-count
定义为在页面加载后 1 秒检索购物车中的商品数量的元素:
<span id="cart-count" hx-get="/cart/count"
hx-trigger="load delay:1s" hx-target="#cart-count" hx-swap="outerHTML">
</span>
我也有一个add-to-cart
按钮,在它自己的东西之后(例如将其自身更改为remove from cart
使用 htmx),发送额外的属性
response['HX-Trigger-After-Swap'] = "cart-updated"
到前端(参见here https://htmx.org/headers/x-hx-trigger/对于文档)。
我有一个有效的事件侦听器,
document.body.addEventListener("cart-updated",
function (evt) {
alert("cart updated")
})
但我怎样才能触发hx-get
of the #cart-count
JS 中的元素?
注意
hx-trigger="load delay:1s, cart-updated"
会工作 https://github.com/bigskysoftware/htmx/issues/187如果购物车的东西在父链中add-to-cart
元素,但不幸的是我的网页并非如此。
注意,这个问题也被问到了here https://github.com/bigskysoftware/htmx/issues/339