Custom attributes - Yea or nay?

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

    Nay. Try something like this instead:

    <div id="foo"/>
    
    <script type="text/javascript">
      document.getElementById('foo').myProperty = 'W00 H00! I can add JS properties to DOM nodes without using custom attributes!';
    </script>
    
    0 讨论(0)
  • 2020-11-22 03:45

    For complex web apps, I drop custom attributes all over the place.

    For more public facing pages I use the "rel" attribute and dump all my data there in JSON and then decode it with MooTools or jQuery:

    <a rel="{color:red, awesome:true, food: tacos}">blah</a>
    

    I'm trying to stick with HTML 5 data attribute lately just to "prepare", but it hasn't come naturally yet.

    0 讨论(0)
提交回复
热议问题