JQuery单击事件删除ul li元素

JQuery单击事件删除ul li元素,jquery,Jquery,我有一个UL LI列表,当用户单击LI时,LI旁边会显示一个按钮。“选定”类也将分配给li。 当用户单击另一个LI时,应删除带有按钮的前一个LI,该按钮将显示在新单击的LI上。然而,我似乎无法删除上一页的按钮 我只想从以前的LI中删除按钮,并将按钮保持在新选择的LI。我怎样才能做到这一点?我的代码如下 提前谢谢 $("ul#optionList li").on("click", function(){ var test= $(this).attr('id'); $("ul#option

我有一个UL LI列表,当用户单击LI时,LI旁边会显示一个按钮。“选定”类也将分配给li。 当用户单击另一个LI时,应删除带有按钮的前一个LI,该按钮将显示在新单击的LI上。然而,我似乎无法删除上一页的按钮

我只想从以前的LI中删除按钮,并将按钮保持在新选择的LI。我怎样才能做到这一点?我的代码如下

提前谢谢

$("ul#optionList li").on("click", function(){
  var test= $(this).attr('id'); 
  $("ul#optionList li").removeClass('selected');  
  $(this).addClass('selected');

  // append the button to the LI
   $(this).append("<a href='javascript:void(0);' class='checkanswer2' id='"+test+"'>Check Answer</a>");

   $(this).children("a").remove(); // this will caused all the buttons to be removed, not just removing the previous selected LI

});
$(“ul#optionList li”)。在(“单击”,函数()上{
var test=$(this.attr('id');
$(“ul#optionList li”).removeClass('selected');
$(this.addClass('selected');
//将按钮附加到LI
$(此)。追加(“”);
$(this).children(“a”).remove();//这将导致删除所有按钮,而不仅仅是删除以前选择的LI
});
试试这个:

$("ul#optionList li").on("click", function(){
  var test= $(this).attr('id'); 
  $(".selected").find("a").remove(); 
  $("ul#optionList li").removeClass('selected');   
  $(this).addClass('selected');
  $(this).children("a").remove(); // this will caused all the buttons to be removed, not just removing the previous selected LI
  $(this).append("<a href='javascript:void(0);' class='checkanswer2' id='"+test+"'>Check Answer</a>");
});
$(“ul#optionList li”)。在(“单击”,函数()上{
var test=$(this.attr('id');
$(“.selected”).find(“a”).remove();
$(“ul#optionList li”).removeClass('selected');
$(this.addClass('selected');
$(this).children(“a”).remove();//这将导致删除所有按钮,而不仅仅是删除以前选择的LI
$(此)。追加(“”);
});
试试这个

$("ul#optionList li").on("click", function(){
  var test= $(this).attr('id'); 
  $(this).siblings().removeClass('selected');   // remove other li class
  $(this).addClass('selected'); // add class to clicked li
  $(this).siblings().find("a").remove(); // remove other li a element
  $(this).append("<a href='javascript:void(0);' class='checkanswer2' id='"+test+"'>Check Answer</a>");
});
$(“ul#optionList li”)。在(“单击”,函数()上{
var test=$(this.attr('id');
$(this).sides().removeClass('selected');//删除其他li类
$(this).addClass('selected');//将类添加到单击的li
$(this).sides().find(“a”).remove();//删除其他LIA元素
$(此)。追加(“”);
});

以下代码将解决您的问题

$("ul#optionList li").not(".selected").children("a").remove();
尝试这样做:

$('ul#optionList').find('.checkanswer2').remove(); // remove the other anchors
$(this).append("<a href='javascript:void(0);' class='checkanswer2'...");

谢谢你的方法也行,但我只能接受一种answer@Sylph,没问题。如果你找到了解决方案,就是这样:)@Sylph很高兴帮助你
:-)
谢谢你的帮助,我已经找到了解决方案:)谢谢你的帮助,我已经找到了解决方案:)
$("ul#optionList li").on("click", function(){
  var test= $(this).attr('id'); 
  $("ul#optionList li").removeClass('selected');  
  $(this).addClass('selected');
  $('ul#optionList').find('.checkanswer2').remove(); // remove the other anchors
  $(this).append("<a href='javascript:void(0);' class='checkanswer2' id='"+test+"'>Check Answer</a>");
  $(this).children("a").remove();
});