简体   繁体   English

javascript-当按下内部按钮时,从div中删除元素

[英]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 divcontact-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.

相关问题 当按下按钮时,使用Javascript刷新div内容 - Refresh div content with Javascript when button is pressed 有没有办法知道某个按钮是否在 JavaScript 中的 div 元素内的某个位置被按下? - Is there any way to know if a certain button has been pressed in a certain location inside a div element in JavaScript? 按下按钮时如何将子元素隐藏在 div 元素中? (不隐藏父母) - How do I hide children inside a div element when button is pressed? (without hidding parent) 按下按钮时,获取多个 div 内的表单 - Get form that is inside of multiples div, when a button is pressed 按下按钮时div不会消失 - div is not fading in when button is pressed 按下表单重置按钮时删除javascript元素 - deleting javascript element when form reset button pressed 我可以在该 div 内单击按钮获得 div 元素(由 JavaScript 循环创建)的 position 吗? - Can I get the position of the div element (created by a loop from JavaScript) on a button click inside of that div? 单击该DIV中的IFRAME时,删除DIV元素 - Remove DIV element when IFRAME inside that DIV is clicked 如何使用jquery或javascript从div元素中删除所有以#开头的字符? - How can i remove all characters started with # from inside a div element using jquery or javascript? 按下按钮时访问一个按钮被包裹的div - Accessing a div a button is wrapped in when button is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM