使用jQuery删除链接图像和其他div ID

时间:2013-04-19 19:02:37

标签: javascript jquery

这是我的代码:

jQuery("#test").after("<a id='clearall' href='#' onclick='jQuery(this).remove(); jQuery('.clearselection').remove;'><img src='clearselection.gif'></a>");

当我尝试这个时,我收到语法错误:

jQuery(this).remove(); jQuery(<---

当我点击链接时,我需要删除链接图像并删除该类.clearselection的所有实例。我试图逃避单引号,但这不起作用。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

不使用内联定义单击处理程序,而是使用jquery来处理:

$(document).on("click", "#clearall", function()
{
   jQuery(this).remove(); 
   jQuery('#clearselection').remove(); //btw you were missing the parens here
});

答案 1 :(得分:0)

替换这个:

onclick='jQuery(this).remove(); jQuery(\'.clearselection\').remove();'