javascript - Onclick 删除附加的 Div - JQuery

标签 javascript jquery html css

如何在单击特定 Div 的“X”按钮时删除附加行。

这是 JsFiddle 演示: http://jsfiddle.net/7jE9p/4/

我也在这里提供代码:

HTML:

      <p>
          <a href="javascript:void(0);" class="add_more">Add More</a>
      </p>

<table border="0" width="500" cellspacing="0">
   <tbody class="append_data"></tbody>

<tr>
  <td> 
      <textarea name="description" id="description"></textarea>
  </td> 
</tr> 

</table>    

CSS:

#description{
    width:400px;    
}

.description_text{
    border:#333333 solid 1px;
    width:400px !important;
}

.append_data{
   white-space: pre-wrap;      /* CSS3 */   
   white-space: -moz-pre-wrap; /* Firefox */    
   white-space: -pre-wrap;     /* Opera <7 */   
   white-space: -o-pre-wrap;   /* Opera 7 */    
   word-wrap: break-word;      /* IE */
}

JQuery:

$('.add_more').click(function()
                     {
                        var description = $('#description').val();
                         $(".append_data").append('<div class="description_text">'+description+' <a href="javascript:void(0);">X</a></div><br><br>');

                     });

最佳答案

试试这个,对于动态添加的元素,您需要使用 on() 并在最近的静态元素上进行委托(delegate),如下所示:

$('.append_data').on('click','.description_text a',function(){
     $(this).closest('.description_text').remove();
});

DEMO

您可以添加到 description_text CSS

margin-bottom:10px;

并忽略添加 <br/>追加

DEMO

关于javascript - Onclick 删除附加的 Div - JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18851396/

相关文章:

javascript - 如何从 Bootstrap 的模态形式发布数据?

html - 尝试在没有 CDN 的情况下包含 font-awesome

javascript - DataTables 不显示 JSON 数据

jquery - 使用 jquery 更改文本颜色

javascript - JS - 点击事件无法正常工作

php - 列出 sql 的最后 10 行并将其打印在字幕中

html - 如何在不给出明确尺寸的情况下防止 flexbox 溢出

javascript - 显示共享文件 google Drive api

php - 在 codeigniter 中使用 javascript 进行表单验证

javascript - 预检响应在 axios 中没有 HTTP ok 状态