Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery所有数据属性的每个循环_Javascript_Jquery_Loops_Attributes_Each - Fatal编程技术网

Javascript jQuery所有数据属性的每个循环

Javascript jQuery所有数据属性的每个循环,javascript,jquery,loops,attributes,each,Javascript,Jquery,Loops,Attributes,Each,我正在尝试在动画之后重置数据属性,并且在应用中遇到了一些问题 不知道我错过了什么。对于每个数据属性等,从理论上讲似乎是可行的 更新: 值得一提的是,数据键都是不同的。例如,data-1=“abc”,data-2=“abc”等,因此需要一个for循环,该循环只查找数据属性 HTML 砰,明白了。脚本有很多开销,因此在用户等待的实例中运行它不是一个选项,依我看。您可以通过特殊性来改进它,而不是使用*选择器 JavaScript(jQuery): var计数器=1;//对于您的实现来说,这不是必需的

我正在尝试在动画之后重置数据属性,并且在应用中遇到了一些问题

不知道我错过了什么。对于
每个
数据
属性等,从理论上讲似乎是可行的


更新:

值得一提的是,
数据
键都是不同的。例如,
data-1=“abc”
data-2=“abc”
等,因此需要一个
for
循环,该循环只查找
数据
属性

HTML


砰,明白了。脚本有很多开销,因此在用户等待的实例中运行它不是一个选项,依我看。您可以通过特殊性来改进它,而不是使用
*
选择器

JavaScript(jQuery):

var计数器=1;//对于您的实现来说,这不是必需的,使用它来调整数字数据键
$('*')。每个(函数(){//查询所有选择器并运行一个循环
var thiis=$(此),
dataAttr=this.data(),
我
if(dataAttr){//如果元素有数据(不考虑属性)
var newAttrs=[];//用于元素的新数据值
$.each(dataAttr,function(key,value){//遍历每个数据对象
var newKey=键+计数器,//计算新数据键
newAttr=[newKey,value];//推送新数据集
newAttrs.push(newAttr);//推送到元素新属性数组
提斯
.removeData(key)//删除数据
.removeAttr('data-'+键);//删除属性(不必要)
});
对于(i=0;i
需要更多详细信息。!请发布您的html。@RajaprabhuAravindasamy谢谢您的回复。我的应用程序中的HTML发生了变化,与文章无关-只是想寻找在for循环
$('*')中收集所有数据属性的理论答案。data()
是问题所在。。。您需要针对单个元素。。。因此,将
*
更改为其他内容,即针对某个特定数据属性或全部…数据属性与
data()
api之间也存在差异。。。数据api可能会使用
data-*
属性初始化值,之后使用数据api所做的更改将不会反映在属性中
var total = 0;    
$.each($('*').data(), function(key, value) {        
    if (key){    
        var thiis = $(this);            
        total += key;            
        thiis.removeData();            
        thiis.data(total, value);
    }
});
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)
        }
    }
});