[英]javascript - remove element from div when button inside is pressed
I have a div
that looks like this: 我有一个看起来像这样的
div
:
<div id="contact-segments">
<div class="contact-segment-item doesnt-include">
<div class="segment-content pull-left" style="width: 80%">
<p>LOL</p>
</div>
<div class="pull-right">
<a href="your link here">
<i class="white-segment-icon fa fa-times"></i>
</a>
</div>
</div>
</div>
Whenever someone presses an add button, I add another contact-segment-item div
to the contact-segments div
through JavaScript with this function: 每当有人按下添加按钮时,我都会使用以下功能通过JavaScript将另一个
contact-segment-item div
到contact-segments div
:
function createUserSegment(tags) {
var div = document.createElement("div");
div.className = 'contact-segment-item includes'
div.innerHTML = "<div class='segment-content pull-left' style='width: 80%''> <p>" + tags + "</p> </div> <div class='pull-right'> <a href='your link here'> <i class='white-segment-icon fa fa-times'></i></a> </div>";
document.getElementById("contact-segments").appendChild(div);
}
As you can see, the divs that are getting added through JavaScript have an a
tag that shows a button with an "x". 如您所见,通过JavaScript添加的div具有
a
标记, a
标记显示带有“ x”的按钮。
How can I remove the contact-segment-item
when the "x" is pressed inside of it? 当按下“ x”按钮时,如何删除它的
contact-segment-item
?
Here's how each of them look so it's easier to picture. 这是每个人的外观,因此更容易描绘。
I can link the "x" button click to javascript but how do I know which child of contact-segments
to delete and also how do I get the p
of it before it's deleted. 我可以将“ x”按钮的点击链接到javascript,但是我如何知道要删除的
contact-segments
哪个子contact-segments
,以及如何在删除之前获得它的p
。
When the user presses the "x" on this
div
, I want to get the p
or in this case Woop!
当用户在此
div
上按下“ x”时,我想获取p
或在这种情况下获得Woop!
so I can do something with it but then also delete that contact-segment-item
所以我可以用它做点什么,然后删除那个
contact-segment-item
Thanks 谢谢
Instead of using .innerHTML
we nest nodes with appendChild.
我们不使用
.innerHTML
而是使用appendChild.
嵌套节点appendChild.
Finally for our close i
button we add onClick
event handler. 最后,对于关闭
i
按钮,我们添加onClick
事件处理程序。 We pass there our div
node, and use remove()
method to remove the node. 我们将
div
节点传递到那里,并使用remove()
方法删除该节点。
EDIT: 编辑:
Added css. 添加了CSS。
Do not use a
if your anchors only needs to delete your segments. 不要使用
a
,如果你的锚只需要删除片段。 For example use only i
without wrapping a
and add a cursor: pointer
style to it. 例如,仅使用
i
而不包裹a
并向其添加cursor: pointer
样式。
See working example: 请参阅工作示例:
function createUserSegment(tags){ var div = document.createElement("div"); div.className = 'contact-segment-item includes'; var tagInfo = document.createElement("div"); tagInfo.className = 'contact-segment-item__text'; tagInfo.innerHTML = tags; var closeButton = document.createElement("i"); closeButton.className = 'contact-segment-item__closeButton white-segment-icon fa fa-times'; closeButton.onclick = function() { div.remove(); }; div.appendChild(tagInfo); div.appendChild(closeButton); document.getElementById("contact-segments").appendChild(div); }
#contact-segments { max-width:350px; width: 100%; } .contact-segment-item { display: block; position: relative; width: 100%; background: #00B792; border-radius: 8px; line-height: 40px; clear: both; padding: 20px 30px; margin-bottom: 10px; } .contact-segment-item__anchor::after { clear: both; } .contact-segment-item__text { display: inline-block; color: #fff; } .contact-segment-item__closeButton { display: inline-block; cursor: pointer; color: #fff; position: absolute; top: 50%; right: 20px; transform: translateY(-50%); }
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" /> <button id="add-new" onClick="createUserSegment('new one')">Add new segment</button> <br/> <div id="contact-segments"> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.