Jquery 删除激发的元素的ID或文本
是否有任何方法可以删除激发的元素的ID或文本。目前,在按钮上激发会添加/删除所选文本,但如果我们单击了多个,则单击按钮中的文本会添加到列表中,当我再次单击所选按钮时,会从列表中删除所有文本,但我只需要删除所选文本。有解决方案吗 jQueryJquery 删除激发的元素的ID或文本,jquery,Jquery,是否有任何方法可以删除激发的元素的ID或文本。目前,在按钮上激发会添加/删除所选文本,但如果我们单击了多个,则单击按钮中的文本会添加到列表中,当我再次单击所选按钮时,会从列表中删除所有文本,但我只需要删除所选文本。有解决方案吗 jQuery $(文档).ready(函数(){ var txt=“”; $(“#_按钮”).children().toggle(函数()){ txt=$(this.text(); $(this.css(“背景”、“蓝色”); $(“#place”)。追加(“+txt+”
$(文档).ready(函数(){
var txt=“”;
$(“#_按钮”).children().toggle(函数()){
txt=$(this.text();
$(this.css(“背景”、“蓝色”);
$(“#place”)。追加(“+txt+”);
},函数(){
$(“#place”).children(this.remove();
$(this.css(“背景”、“红色”);
});
});
HTML
- a
- b
- c
您可以查找正在切换的元素文本,并使用:contains()
选择器使用以下代码仅删除相应的td
改变这个
$("#place").children(this).remove();
到
演示:
阅读有关的详细信息您可以使用jquery contains选择器选择与单击的列表项具有匹配文本的表格单元格:
txt=$(this).text();
$("#place").children(":contains("+ txt +")").remove();
我为它制作了一把小提琴,以显示它在工作。像这样的东西
$(document).ready(function() {
var txt="";
$("#_button li").toggle(function(){
txt=$(this).text();
$(this).css("background","blue");
var id = $(this).attr('id');
$("#place").append("<td class="+id+">"+txt+"></td>");
},function() {
var id = $(this).attr('id');
$("#place").find('td.' + id).remove();
$(this).css("background","red");
});
});
$(文档).ready(函数(){
var txt=“”;
$(“##_按钮li”).切换(函数(){
txt=$(this.text();
$(this.css(“背景”、“蓝色”);
var id=$(this.attr('id');
$(“#位置”)。追加(“+txt+”>”);
},函数(){
var id=$(this.attr('id');
$(“#place”).find('td.'+id).remove();
$(this.css(“背景”、“红色”);
});
});
以下代码将完成此操作。下面是要使用的JSFIDLE:
$(文档).ready(函数()
{
var txt=“”;
$(“#_按钮”).children().toggle(函数())
{
txt=$(this.text();
$(this.css(“背景”、“蓝色”);
$(“#place”)。追加(“+txt+”);
},函数()
{
txt=$(this.text();
$(“#place”).children(“td:contains(“+txt+”))”).remove();
$(this.css(“背景”、“红色”);
});
});
$("#place").children(':contains('+$(this).text()+')').remove();
txt=$(this).text();
$("#place").children(":contains("+ txt +")").remove();
$(document).ready(function() {
var txt="";
$("#_button li").toggle(function(){
txt=$(this).text();
$(this).css("background","blue");
var id = $(this).attr('id');
$("#place").append("<td class="+id+">"+txt+"></td>");
},function() {
var id = $(this).attr('id');
$("#place").find('td.' + id).remove();
$(this).css("background","red");
});
});
$(document).ready(function()
{
var txt = "";
$("#_button").children().toggle(function()
{
txt = $(this).text();
$(this).css("background","blue");
$("#place").append("<td>"+txt+"</td>");
},function()
{
txt = $(this).text();
$("#place").children("td:contains('"+txt+"')").remove();
$(this).css("background","red");
});
});