jQuery doesn't returns the new data-attribute

前端 未结 2 1407
误落风尘
误落风尘 2021-01-23 07:00

I\'m trying a very simple task working with data-attr and jQuery, check this example:

Markup:

相关标签:
2条回答
  • 2021-01-23 07:42

    Working demo http://jsfiddle.net/DDv8U/

    • API: http://api.jquery.com/data/ - try using this $(div).data('name', ' - Testing 2');

    rest should fit the need :)

    code

    $(function(){
        var div = $('div');
        $(div).append($(div).data('name'));
        $(div).data('name', ' -  Testing 2');
        $(div).append($(div).data('name'));
    });
    
    0 讨论(0)
  • 2021-01-23 07:45

    You are changing the attribute but this doesn't automatically update the data. This works:

    $(function(){
        var div = $('div');
        div.append(div.data('name'));
        div.data('name', ' -  Testing 2');
        div.append(div.data('name'));
    });
    
    0 讨论(0)
提交回复
热议问题