因此,我正在创建一个应用程序,用于记录咖啡馆客户的交易,并且我正在当前的订单页面上工作,并且上面会有一些框来显示这些订单。我对 javascript 非常无能,所以我不知道当这个人完成订单时如何在我的框中添加关闭按钮。有人可以帮我解决这个问题吗?谢谢。这是我的代码
#mydiv {
position: absolute;
z-index: 9;
background-color: #f1f1f1;
text-align: center;
border: 1px solid #d3d3d3;
}
<div id="mydiv">
<div id="mydivheader">Order Num#</div>
<p>Order Items</p>
<span id='close' onclick='this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;'>x</span>
</div>
<div id="mydiv2">
<div id="mydivheader">Order Num#</div>
<p>Order Items</p>
</div>
<div id="mydiv3">
<div id="mydivheader">Order Num#</div>
<p>Order Items</p>
</div>
<script>
//Make the DIV element draggagle:
dragElement(document.getElementById("mydiv"));
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "header")) {
/* if present, the header is where you move the DIV from:*/
document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
} else {
/* otherwise, move the DIV from anywhere inside the DIV:*/
elmnt.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
最佳答案
onClick 接受函数。尝试创建一个函数来隐藏卡片。然后在单击按钮时调用该函数。
使用remove
可以更轻松地完成此操作。
例如..
function orderCompleted(event) {
event.target.parentElement.classList.add('hide-card')
}
.card {
border: 1px solid #333;
margin: 5px;
padding: 5px;
}
.hide-card {
display: none;
}
<div id="card-1" class="card">
<h3>Order Number: 123</h3>
<ul>
<li>Burger</li>
<li>Diet coke</li>
</ul>
<button onclick="orderCompleted(event)">Completed</button>
</div>
<div id="card-2" class="card">
<h3>Order Number: 124</h3>
<ul>
<li>Pizza</li>
<li>Hot chocolate</li>
</ul>
<button onclick="orderCompleted(event);">Completed</button>
</div>
上面将从当前 View 中隐藏卡片。我希望您没有使用订单的静态数据 (.json)。在这种情况下,每次您重新加载页面时,隐藏的卡片都会重新出现,因为我们不会更新已完成的订单。如果您在您正在做的事情范围内没有看到这一点,请忽略它。
关于javascript - 我的 javascript 框上需要一个关闭按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52163984/