【发布时间】:2022-01-13 13:11:35
【问题描述】:
在我的 WooCommerce 主题中,我想在 WooCommerce 使用 AJAX 向元素动态添加“加载”类时显示微调器图标。
我尝试使用 Alpine.js 的 $el 属性来检索当前的 DOM 节点,但这不起作用。它也没有“观看” 的 classList。
如何使用 Alpine.js 完成此任务?
<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="ajax_add_to_cart add_to_cart_button single_add_to_cart_button button alt flex justify-center" data-product_id="<?php echo get_the_ID(); ?>">
<!-- Spinner Icon -->
<svg x-show="$el.parentElement.classList.contains('loading')" class="animate-spin py-1 h-7 w-7 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
...
</svg>
<!-- Toevoegen aan winkelmand -->
<span x-show="$el.parentElement.classList.contains('!loading')">
<?php echo esc_html( $product->single_add_to_cart_text() ); ?>
</span>
</button>
【问题讨论】:
-
此方法不起作用,因为 classList 不是响应式的。您需要将事件侦听器附加到加载函数调度的事件。你能展示执行 AJAX 调用的函数吗?
-
我正在使用 WooCommerce 类:ajax_add_to_cart add_to_cart_button,它启用了 AJAX 功能。
标签: javascript woocommerce alpine.js alpinejs