获取数据属性值onclick链接/按钮 [英] get data attribute value onclick link / button

查看:81
本文介绍了获取数据属性值onclick链接/按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<span class="btn btn-block btn-inverse btn-icon glyphicons home" id="changeb" data-datac="daily"><i></i>Daily</span>
            <span class="btn btn-block btn-inverse btn-icon glyphicons home" id="changeb" data-datac="1week"><i></i>1 Week</span>
            <span class="btn btn-block btn-inverse btn-icon glyphicons home" id="changeb" data-datac="monthly"><i></i>Month</span>
            <span class="btn btn-block btn-inverse btn-icon glyphicons home" id="changeb" data-datac="3Month"><i></i>3 Month</span>

jquery是

$('#changeb').click(function() {

        var d = $(this).data('datac');      
        alert(d);   
} );

我需要获取click事件中的data-datac值

I need to get the data-datac value on click event ,

推荐答案

您的代码很好,因为您具有相同ID的多个元素是无效的.该事件将被绑定到 first 元素,并且在DOM中具有给定的ID.使用公共类而不是id绑定事件.您可以使用类选择器来绑定事件.

Your code is fine you have same id for more then one element which is not valid. The event will be binded to first element with given id in the DOM. Use common class instead of id to bind the event. You can use class selector to bind the event.

在线演示

Live Demo

$('.btn').click(function() {
      var d = $(this).data('datac');      
      alert(d);   
});

这篇关于获取数据属性值onclick链接/按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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