我有一个UL LI列表,当用户单击LI时,将使用append在LI旁边显示一个按钮。 “选择”类也将分配给李。 当用户点击另一个LI时,应该删除先前的LI with按钮,并且该按钮将出现在新点击的LI上。但是我似乎无法删除prev LI的按钮。JQuery单击事件删除ul 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
});
提供您的HTML。 –