弹出模式仅适用于第一项
Pop Up modal only works on first item
我在 shopify 商店工作,购物车页面上有一个 'remove from cart' 选项,当他们点击它时会出现一个弹出窗口,以确认他们是否要将其从购物车中删除。
我遇到的问题是弹出窗口仅适用于第一个项目,其他项目只会将您带回到页面顶部。
这是我正在使用的代码。
删除文本
<td style="text-align: center">
<a href="#" id="cart_popup">X</a>
</td>
弹出窗口
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<p class="modal-index-title">Hey!</p>
<p class="modal-title">Are you sure you want to remove this?</p>
<div class="modal-confirm">
<span class="close">No!</span>
<p><a href="#" onclick="remove_item({{ item.variant.id }}); return false;">Yes</a></p>
</div>
</div>
</div>
剧本
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("cart_popup");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
ID 对于 DOM 元素应该是唯一的,因此您的 JS 代码将只获取 cart_popup
元素的第一个实例并在那里应用 onClick。
如果您想让它应用于所有移除购物车按钮,请向其添加 CSSclass,然后将 btn.onclick
功能应用于具有 class 的按钮。
编辑:
示例:
var classname = document.getElementsByClassName("classname");
var showModal = function() {
modal.style.display = "block";
};
for (var i = 0; i < classname.length; i++) {
classname[i].addEventListener('click', showModal, false);
}
您可能有多个具有相同 id
属性的元素。只有第一个会触发您的 btn.onclick
函数。
我在 shopify 商店工作,购物车页面上有一个 'remove from cart' 选项,当他们点击它时会出现一个弹出窗口,以确认他们是否要将其从购物车中删除。 我遇到的问题是弹出窗口仅适用于第一个项目,其他项目只会将您带回到页面顶部。
这是我正在使用的代码。
删除文本
<td style="text-align: center">
<a href="#" id="cart_popup">X</a>
</td>
弹出窗口
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<p class="modal-index-title">Hey!</p>
<p class="modal-title">Are you sure you want to remove this?</p>
<div class="modal-confirm">
<span class="close">No!</span>
<p><a href="#" onclick="remove_item({{ item.variant.id }}); return false;">Yes</a></p>
</div>
</div>
</div>
剧本
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("cart_popup");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
ID 对于 DOM 元素应该是唯一的,因此您的 JS 代码将只获取 cart_popup
元素的第一个实例并在那里应用 onClick。
如果您想让它应用于所有移除购物车按钮,请向其添加 CSSclass,然后将 btn.onclick
功能应用于具有 class 的按钮。
编辑:
示例:
var classname = document.getElementsByClassName("classname");
var showModal = function() {
modal.style.display = "block";
};
for (var i = 0; i < classname.length; i++) {
classname[i].addEventListener('click', showModal, false);
}
您可能有多个具有相同 id
属性的元素。只有第一个会触发您的 btn.onclick
函数。