简体   繁体   English

如何删除元素上的所有数据属性?

[英]How to remove all data attributes on element?

I've been checking many and many posts here in stackoverflow but, even if many solutions should be working, it yet doesn't work for me. 我一直在stackoverflow中检查很多很多帖子,但是,即使许多解决方案应该正常工作,它仍然不适合我。

I have a modal in bootstrap where I have a list of bootstrap checkboxes ( <label><input> style). 我在bootstrap中有一个模态,其中有一个bootstrap复选框列表( <label><input> style)。

Each checkbox has a set of data attributes that are directly setted from an AJAX request and, therefore, that aren't settet through jQuery. 每个复选框都有一组数据属性, 这些属性是从AJAX请求直接设置的 ,因此不是通过jQuery设置的。

In my situation, I need to remove all the data-attr from the checkbox when it is toggled and add some other. 在我的情况下,我需要在切换时从复选框中删除所有data-attr并添加其他数据。

The problem, however, is that I'm being unable to remove them. 然而,问题在于我无法删除它们。

I've first tried using jQuery.removeData() , but then I've read that it actually only removes data attributes if at least one of them has been set through jQuery , therefore it is not my case. 我首先尝试使用jQuery.removeData() ,但后来我读到它实际上只删除了数据属性,如果至少有一个已经通过jQuery设置 ,因此它不是我的情况。

So, I've checked a little bit around and found out that I should be using jQuery.removeAttr() prototype and remove each data element. 所以,我已经检查了一下,发现我应该使用jQuery.removeAttr()原型并删除每个数据元素。

So, my case is this one: 所以,我的情况是这样的:

$('.modal_day_checkbox').on('change', function(){
        if ($(this).is(':checked')) {
            removeAllData($(this));
            $(this).data('newid', 'test_id');
            console.log($(this).data());
        }
        else {
            removeAllData($(this));
            $(this).data('testID', 'ID_NEW');
            console.log($(this).data());
        }
    });

And, in order to have a clean code, I've implemented the function removeAllData: 并且,为了获得干净的代码,我实现了函数removeAllData:

function removeAllData(object) {
        $.each(object.data(), function(i, v) {
            object.removeAttr("data-"+i);
            console.log('removing: '+'data-'+i);
        });
    }

Which is just looping through the object to check what data attributes it has and it should be removing it. 这只是循环遍历对象以检查它具有哪些数据属性,它应该删除它。

Surprisingly, the result is this one: 令人惊讶的是,结果是这样的:

Any idea? 任何的想法?

Try calling the jQuery .removeData function as well as your removeAllData function: 尝试调用jQuery .removeData函数以及removeAllData函数:

function removeAllData(object) {
    $.each(object.data(), function(i, v) {
        object.removeAttr("data-"+i);
        console.log('removing: '+'data-'+i);
    });
    object.removeData();  // clear the cache too
}

jQuery maintains a separate object where data-foo values are cached as soon as they're access (and where writes to .data are stored) and that cache is not cleared when you call .removeAttr('data-foo') . jQuery维护一个单独的对象,其中data-foo值在访问时(以及存储写入 .data位置)被缓存,并且当您调用.removeAttr('data-foo')时不会清除缓存。

Chosen solution works great, but will break on multi-dash attributes (like data-one-two ) because: 选择的解决方案效果很好,但会破坏多破折号属性(如data-one-two ),因为:

Since jQuery 3, every two-character sequence of "-" (U+002D) followed by a lowercase ASCII letter in a key is replaced by the uppercase version of the letter, in alignment with the HTML dataset API. 从jQuery 3开始,每个双字符序列“ - ”(U + 002D)后跟一个键中的小写ASCII字母将被字母的大写版本替换,与HTML数据集API保持一致。 A statement like $( "body" ).data( { "my-name": "aValue" } ).data(); $( "body" ).data( { "my-name": "aValue" } ).data(); will return { myName: "aValue" } . 将返回{ myName: "aValue" }

source 资源

To make results of data() work with removeAttr() we should cast them back to normal: 要使data()结果与removeAttr()我们应该将它们恢复正常:

function removeAllData(object) {
    $.each(object.data(), function(i, v) {
        i = i.replace(/([A-Z])/g, function(all, letter) {
            return '-' + letter.toLowerCase()
        });  // fix multi-dash attributes
        object.removeAttr("data-"+i);
        console.log('removing: '+'data-'+i);
    });
    object.removeData();  // clear the cache too
}

It's not an ideal solution, but should work in most cases. 它不是一个理想的解决方案,但在大多数情况下应该可以使用。

this is wrong 这是错的

object.removeAttr("data-"+i);

// you have to pass in the name of the attribute //你必须传递属性的名称

Do this instead 这样做

 $.each(object.data(), function(i, v) {
            object.removeAttr(v);
            console.log('removing: '+v);
        });

// Removed quote //删除了报价

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM