本文介绍了无法调用动态添加的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过ajax调用填充数据的页面中完成删除。 我的页面在加载数据之前有这个:
I am trying to accomplish delete in page which is filled with data by ajax call. my page has this before data is loaded:
<span id="ShowSelectedCategories"></span>在ajax通话后我填写以下信息:
and after ajax call i fill with below info:
function(data){ $('#ShowSelectedCategories').prepend('<a href="" id="'+b.SelectedCategoriesID+'" class="DeleteCat"> x <span style="color:red">' + data +'</span> </a>  '); }最后点击后我想删除点击的元素,但它不起作用。
and finally after click i want to remove clicked element but it doesn't work.
$(function(){ $("#ShowSelectedCategories").click(function(e){ e.target.outerHTML //this is the part i want to remove after click e.preventDefault(); })我试过 $(。DeleteCat).click()但这给出了错误,因为具有该类的元素是动态创建的。
I have tried $(".DeleteCat).click() but this gave error because elements with that class was created dynamically.
感谢您的帮助
推荐答案$('#ShowSelectedCategories').on('click', '.DeleteCat', function(e){ $(this).remove(); e.preventDefault(); });
jsfiddle/Dj5NQ/
更多推荐
无法调用动态添加的类
发布评论