在jQuery中使用数据属性 [英] Using data attributes with jQuery

查看:64
本文介绍了在jQuery中使用数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个按钮:

<button type="button" class="themeChanger" data-themeValue="grid" value="Grid">
   <img src="templateImages/Icon_200.png" />                
</button>

还有这个jQuery:

And this jQuery:

$(".themeChanger").click(function () { 
    alert($(this).attr("data-themeValue")); 
    alert($(this).data("themeValue")); 
});

由于某种原因,第一个警报显示应有的网格",但第二个警报未定义.我有什么愚蠢的东西想念吗?

For some reason the first alert shows "grid" like it should, but the second shows undefined. Is there something stupid I'm missing?

推荐答案

我认为数据将以小写字母显示:alert($(this).data("themevalue")) //grid

I think data will look on lowercases: alert($(this).data("themevalue")) //grid

或者如果您想使用themeValue,则需要使用:

or if you want to use themeValue you need to use:

我错了,它与小写字母无关,如果您具有以下属性,则可以使用themeValue:data-theme-value然后使用$(element).data("themeValue")

I was wrong, it doesnt have anything to do with lowercases, you can use themeValue if you are having the attribute: data-theme-value then you call itwith $(element).data("themeValue")

<button class="themeChanger" data-themeValue="Theme1" data-theme-value="Theme2"></button>

$(".themeChanger").click(function() {
    var el = $(this);

    alert($(this).data("themeValue")); //Theme2
    alert($(this).data("themevalue")); //Theme1
});

这篇关于在jQuery中使用数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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