无法调用动态添加的类

编程入门 行业动态 更新时间:2024-10-22 08:32:23
本文介绍了无法调用动态添加的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试通过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>&nbsp '); }

最后点击后我想删除点击的元素,但它不起作用。

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/

更多推荐

无法调用动态添加的类

本文发布于:2023-11-11 18:11:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579196.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!