最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery each loop of all data attributes - Stack Overflow

programmeradmin1浏览0评论

I'm trying to reset data attributes after an animation and am running through some trouble applying the technique from answer 2 of this post.

Not sure what I'm missing here. Seems theoretically feasible to say for each data attribute, etc.


UPDATE:

Worth mentioning that the data keys are all different. E.g. data-1="abc", data-2="abc", etc, hence the need for a for loop that simply looks for data attributes.

HTML

var total = 0;    
$.each($('*').data(), function(key, value) {        
    if (key){    
        var thiis = $(this);            
        total += key;            
        thiis.removeData();            
        thiis.data(total, value);
    }
});

I'm trying to reset data attributes after an animation and am running through some trouble applying the technique from answer 2 of this post.

Not sure what I'm missing here. Seems theoretically feasible to say for each data attribute, etc.


UPDATE:

Worth mentioning that the data keys are all different. E.g. data-1="abc", data-2="abc", etc, hence the need for a for loop that simply looks for data attributes.

HTML

var total = 0;    
$.each($('*').data(), function(key, value) {        
    if (key){    
        var thiis = $(this);            
        total += key;            
        thiis.removeData();            
        thiis.data(total, value);
    }
});
Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked Jan 20, 2014 at 6:34 technopeasanttechnopeasant 7,95933 gold badges93 silver badges151 bronze badges 7
  • 1 More details needed.! Please post your html. – Rajaprabhu Aravindasamy Commented Jan 20, 2014 at 6:36
  • @RajaprabhuAravindasamy Thanks for the reply. The HTML in my application changes and is irrelevant to the post - just looking for a theoretical answer on gathering all data attributes within a for loop – technopeasant Commented Jan 20, 2014 at 6:38
  • 1 $('*').data() is the problem... you need to target a singe element... so change * to something else – Arun P Johny Commented Jan 20, 2014 at 6:38
  • are you targeting a particular data attribute or all... – Arun P Johny Commented Jan 20, 2014 at 6:39
  • 1 also there is a difference between the data attribute and the data() api... the data api might use the data-* attribute to initialize the value after that the changes made using data api will not be reflected in the attribute – Arun P Johny Commented Jan 20, 2014 at 6:41
 |  Show 2 more ments

1 Answer 1

Reset to default 3

Boom, got it. The script has a lot of overhead, so running it in an instance that a user will wait through isn't an option, IMO. You could improve it with specificity instead of the * selector.

JavaScript (jQuery):

var counter  = 1; // not necessary for your implementation, using it to adjust numeric data keys

$('*').each(function(){ // query all selectors and run through a loop

    var thiis    = $(this),
        dataAttr = thiis.data(),
        i;

    if (dataAttr) { // if the element has data (regardless of attribute)

        var newAttrs = []; // for the element's new data values

        $.each(dataAttr, function(key, value) { // loop through each data object

            var newKey  = key + counter, // calculate new data key
                newAttr = [newKey, value]; // push the new data set

            newAttrs.push(newAttr); // push to elements new attributes array

            thiis
                .removeData(key) // remove the data
                .removeAttr('data-' + key); // remvoe the attribute (unnecessary)
        });

        for (i = 0; i < newAttrs.length; i++) { // for each new attribute

            thiis.data(newAttrs[i][0], newAttrs[i][1]); // add the data
            thiis.attr('data-' + newAttrs[i][0], newAttrs[i][1]); // add the attribute (unnecessary)
        }
    }
});
发布评论

评论列表(0)

  1. 暂无评论