如何将数据属性传递给函数?

时间:2017-01-04 12:44:09

标签: javascript jquery

有几个a元素

<a class="gift-info" data-giftid="1" ...
<a class="gift-info" data-giftid="12" ...
<a class="gift-info" data-giftid="99" ...

如何获取data-giftid属性并将值传递给函数?

$(document).ready(function() {
    $("a.gift-info").click(function(){
        // how to pass data-giftid value into infoHide() function?
        var _id= ...
        infoHide(_id);
    });
});

1 个答案:

答案 0 :(得分:0)

$(document).ready(function() {
   var $ele = $("a.gift-info");
  $.each($ele,function(index,element){
    element.click(function(){
        // how to pass data-giftid value into infoHide() function?
        var _id= $(this).attr('data-giftid')
        infoHide(_id);
    });
  
  }):
    
});