Custom attributes - Yea or nay?

前端 未结 14 1716
南旧
南旧 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:22

    The easiest way to avoid use of custom attributes is to use existing attributes.

    use meaningful, relevant class names.
    For example, do something like: type='book' and type='cd', to represent books and cds. Classes are much better for representing what something IS.

    e.g. class='book'

    I have used custom attributes in the past, but honestly, there really isn't a need to for them if you make use of existing attributes in a semantically meaningful way.

    To give a more concrete example, let's say you have a site giving links to different kinds of stores. You could use the following:

    <a href='wherever.html' id='bookstore12' class='book store'>Molly's books</a>
    <a href='whereverelse.html' id='cdstore3' class='cd store'>James' Music</a>
    

    css styling could use classes like:

    .store { }
    .cd.store { }
    .book.store { }
    

    In the above example we see that both are links to stores (as opposed to the other unrelated links on the site) and one is a cd store, and the other is a book store.

    0 讨论(0)
  • 2020-11-22 03:22

    We've made a web-based editor that understands a subset of HTML - a very strict subset (that understood nearly universally by mail clients). We need to express things like <td width="@INSWIDTH_42@"> in the database, but we can't have that in the DOM, otherwise the browser where the editor runs, freaks out (or is more likely to freak out than it is likely to freak out over custom attributes). We wanted drag-and-drop, so putting it purely in the DOM was out, as was jquery's .data() (the extra data didn't get copied properly). We probably also needed the extra data to come along for the ride in .html(). In the end we settled on using <td width="1234" rs-width="@INSWIDTH_42@"> during the editing process, and then when we POST it all, we remove width and do a regex search-and-destroy s/rs-width=/width=/g.

    At first the guy writing most of this was the validation-nazi on this issue and tried everything to avoid our custom attribute, but in the end acquiesced when nothing else seemed to work for ALL our requirements. It helped when he realized that the custom attribute would never appear in an email We did consider encoding our extra data in class, but decided that would be the greater of two evils.

    Personally, I prefer to have things clean and passing validators etc., but as a company employee I have to remember that my primary responsibility is advancing the company's cause (making as much money as quickly as possible), not that of my egotistical desire for technical purity. Tools should work for us; not us for them.

    0 讨论(0)
  • 2020-11-22 03:26

    I'm not doing using custom attributes, because I'm outputing XHTML, because I want the data to be machine-readable by 3rd-party software (although, I could extend the XHTML schema if I wanted to).

    As an alternative to custom attributes, mostly I'm finding the id and class attributes (e.g. as mentioned in other answers) sufficient.

    Also, consider this:

    • If the extra data is to be human-readable as well as machine-readable, then it needs to be encoded using (visible) HTML tags and text instead of as custom attributes.

    • If it doesn't need to be human readable, then perhaps it can be encoded using invisible HTML tags and text.

    Some people make an exception: they allow custom attributes, added to the DOM by Javascript on the client side at run-time. They reckon this is OK: because the custom attributes are only added to the DOM at run-time, the HTML contains no custom attributes.

    0 讨论(0)
  • 2020-11-22 03:27

    Custom attributes, in my humble opinion, should not be used as they do not validate. Alternative to that, you can define many classes for a single element like:

    <div class='class1 class2 class3'>
        Lorem ipsum
    </div>
    
    0 讨论(0)
  • 2020-11-22 03:30

    Spec: Create an ASP.NET TextBox control which dynamically auto-formats its text as a number, according to properties "DecimalSeparator" and "ThousandsSeparator", using JavaScript.


    One way to transfer these properties from the control to JavaScript is to have the control render out custom properties:

    <input type="text" id="" decimalseparator="." thousandsseparator="," />
    

    Custom properties are easily accessible by JavaScript. And whilst a page using elements with custom properties won't validate, the rendering of that page won't be affected.


    I only use this approach when I want to associate simple types like strings and integers to HTML elements for use with JavaScript. If I want to make HTML elements easier to identify, I'll make use of the class and id properties.

    0 讨论(0)
  • 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:

    <a href="test.html" mine-one="great" mine-two="awesome">Test</a>
    

    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;
    });
    
    0 讨论(0)
提交回复
热议问题