无法调用动态添加的类 [英] dynamically added class can't be called

查看:65
本文介绍了无法调用动态添加的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

< a href =http://jsfiddle.net/Dj5NQ/ =nofollow> http://jsfiddle.net/Dj5NQ/

这篇关于无法调用动态添加的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆