如何获取动态生成的元素的数据属性 [英] how to get data attributes of dynamically generated element

查看:90
本文介绍了如何获取动态生成的元素的数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从服务器抛出动态内容(通过AJAX)时,我有以下响应.

while throwing dynamic content (through AJAX) from server, i have following response.

<a data-contentid="1">Some content</a> ...

我已经使用实时绑定了点击事件.问题出在我的点击事件中,我无法 获取数据属性,因此对于我的情况,手动附加不是一个选择.有什么想法吗?

i have binded the click event using live. The problem is in my click event, i am unable to get data attributes, so manually attaching is not an option for my case. any ideas?

示例代码:

 $('a.delContent').live("click",function () {

            var cid= jQuery.data(this, "contentid");

            alert(cid);  //undefined
            return false;
        }
           );

推荐答案

在点击事件处理程序中尝试以下操作:

Try this inside your click event handler:

$(this).data('contentId')

在jsfiddle中工作: http://jsfiddle.net/Mk2zy/

Works in jsfiddle: http://jsfiddle.net/Mk2zy/

这篇关于如何获取动态生成的元素的数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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