jquery怎样获取html页面中的data-xxx

$(this).attr("data-id") // will return the string "123"
or .data() (if you use newer jQuery >= 1.4.3)

$(this).data("id") // will return the number 123

注:
1.两种方法 ①使用attr方法获得data-*的值 $(this).attr('data-id');② $(this).data("id")
2. data-* 都可以使用 $(this).data(' * ');获取,比如 data-cat ,使用$(this).data(cat)获取。
3.只能使用中横线 ‘ - ’ !!
posted @ 2017-03-30 15:09  Martin_lee  阅读(702)  评论(0编辑  收藏  举报