简体   繁体   English

从 div 中删除文本

[英]Removing text from a div

I created two buttons.我创建了两个按钮。 One adds a information from an input and the other one removes the information of the input if it exists within the list.一个从输入添加信息,另一个删除输入的信息(如果它存在于列表中)。

<div id = inputArea>

<button id= "add">Add</button><input type="text" name="add"></input>

<button id= "remove">Remove</button><input type="text" name="remove"></input>

</div>
<div id= "box"></div>

I mannaged to write the Jquery for add, but not the remove:我设法编写了用于添加的 Jquery,而不是删除:

$('#add').click(function(){
var toAdd = $('input[name=add]').val();
$('#box').append("<p>" +toAdd+ "</p>");
});

$('#remove').click(function(){
var checkRemove = $('input[name=remove]').val();
(......)
};

Use filter() method to filter out using custom condition.使用filter()方法使用自定义条件过滤掉。

$('#remove').click(function(){
  var checkRemove = $('input[name=remove]').val();
  $('#box p').filter(function(){
    return $(this).html() == checkRemove;
  }).remove();
});

 $('#add').click(function() { var toAdd = $('input[name=add]').val(); $('#box').append("<p>" + toAdd + "</p>"); }); $('#remove').click(function() { var checkRemove = $('input[name=remove]').val(); $('#box p').filter(function() { return $(this).html() == checkRemove; }).remove(); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id=inputArea> <button id="add">Add</button> <input type="text" name="add"> <button id="remove">Remove</button> <input type="text" name="remove"> </div> <div id="box"></div>

you can use replace function to remove the selected word您可以使用替换功能删除选定的单词

$('#add').click(function(){
var toAdd = $('input[name=add]').val();
$('#box').append("<p>" +toAdd+ "</p>");
});

$('#remove').click(function(){
var checkRemove = $('input[name=remove]').val();
var html =$('#box').html();

var res = html.replace("<p>" +checkRemove+ "</p>","");
$('#box').html(res);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM