Jquery 删除DOM元素错误
使用这段代码,我需要删除生成的新元素。它不起作用。firebug中没有出现JS错误Jquery 删除DOM元素错误,jquery,Jquery,使用这段代码,我需要删除生成的新元素。它不起作用。firebug中没有出现JS错误 $('.popular-list li a').live("click",function() //this will apply to all anchor tags { var stuff = $(this).text(); var hasDuplicate = false;
$('.popular-list li a').live("click",function() //this will apply to all anchor tags
{
var stuff = $(this).text();
var hasDuplicate = false;
$('#favoritesdrinks li').each( function(){
if ($(this).text() === stuff ){
hasDuplicate = true;
return false;
}
});
if (hasDuplicate ) {
alert("Already Added") }
else {
$('#favoritesdrinks').append('<li>'+stuff+' --- <a href="javascript:;" class="remove">Remove Item </a> </li>');
}
});
Removal:
$("a.remove").click(function() {
$(this).fadeOut(500, function() { $(this).remove(); });
});
$('.popular list li a').live(“单击”,函数()//这将应用于所有锚定标记
{
var stuff=$(this.text();
var hasdeplicate=false;
$('#favoritedlinks li')。每个(函数(){
if($(this).text()==stuff){
hasdeplicate=true;
返回false;
}
});
如果(重复){
警报(“已添加”)}
否则{
$(“#收藏夹链接”).append(“”+stuff+-- ”);
}
});
删除:
$(“a.remove”)。单击(函数(){
$(this.fadeOut(500,function(){$(this.remove();});
});
该行将删除A链接,而不是LI标记,因为您正在使用$(this)您需要使用.live事件作为带有类remove的锚。此外,它的上下文将是锚点单击中的锚点,因此需要使用.parent()淡出并删除li
$('.popular-list li a').live("click",function() {
var stuff = $(this).text();
var hasDuplicate = false;
$('#favoritesdrinks li').each( function(){
if ($(this).text() === stuff ){
hasDuplicate = true;
return false;
}
});
if (hasDuplicate ) {
alert("Already Added") }
else {
$('#favoritesdrinks').append('<li>'+stuff+' --- <a href="#" class="remove">Remove Item </a> </li>');
}
});
$("a.remove").live('click', function(ev) {
ev.preventDefault();
$(this).parent().fadeOut(500, function(ev) {
$(this).remove();
});
});
$('.popularlist li a').live(“单击”,函数(){
var stuff=$(this.text();
var hasdeplicate=false;
$('#favoritedlinks li')。每个(函数(){
if($(this).text()==stuff){
hasdeplicate=true;
返回false;
}
});
如果(重复){
警报(“已添加”)}
否则{
$(“#收藏夹链接”).append(“”+stuff+-- ”);
}
});
$(“a.remove”).live('点击',功能(ev){
ev.preventDefault();
$(this.parent().fadeOut(500,函数(ev){
$(this.remove();
});
});
这会导致ev未定义错误的无限循环,尽管在这一点上有效。他还需要在“var stuff=$(This).text();”上使用parent(),因为这也与a匹配,而不是LIAGIN。非常感谢你的红方块你是我的救世主
$('.popular-list li a').live("click",function() {
var stuff = $(this).text();
var hasDuplicate = false;
$('#favoritesdrinks li').each( function(){
if ($(this).text() === stuff ){
hasDuplicate = true;
return false;
}
});
if (hasDuplicate ) {
alert("Already Added") }
else {
$('#favoritesdrinks').append('<li>'+stuff+' --- <a href="#" class="remove">Remove Item </a> </li>');
}
});
$("a.remove").live('click', function(ev) {
ev.preventDefault();
$(this).parent().fadeOut(500, function(ev) {
$(this).remove();
});
});