Custom attributes - Yea or nay?

前端 未结 14 1907
南旧
南旧 2020-11-22 03:02

Recently I have been reading more and more about people using custom attributes in their HTML tags, mainly for the purpose of embedding some extra bits of data for use in ja

14条回答
  •  时光取名叫无心
    2020-11-22 03:36

    I know people are against it, but I came up with a super short solution for this. If you want to use a custom attribute like "mine" so for example:

    Test
    

    Then you can run this code to get an object back just like jquery.data() does.

    var custom_props = {} ;
    $.each($(".selector")[0].attributes, function(i,x) {
        if (this.specified && x.name.indexOf("mine-") !== -1) 
            self.new_settings[x.name.replace("modal-","")] = x.value;
    });
    

提交回复
热议问题