Can't update data-attribute value

后端 未结 9 1522
北海茫月
北海茫月 2020-11-27 17:11

Although there are some examples about this on the web, it does not seem to work correctly. I can\'t figure out the problem.

I have this simple html

         


        
9条回答
  •  独厮守ぢ
    2020-11-27 17:22

    Use that instead, if you wish to change the attribute data-num of node element, not of data object:

    DEMO

    $('#changeData').click(function (e) { 
        e.preventDefault();
        var num = +$('#foo').attr("data-num");
        console.log(num);
        num = num + 1;
        console.log(num);
        $('#foo').attr('data-num', num);
    });
    

    PS: but you should use the data() object in virtually all cases, but not all...

提交回复
热议问题