Can I add a custom attribute to an HTML tag?

后端 未结 17 2329
醉话见心
醉话见心 2020-11-22 08:06

Can I add a custom attribute to an HTML tag like the following?


相关标签:
17条回答
  • 2020-11-22 08:28

    use data-any , I use them a lot

    <aside data-area="asidetop" data-type="responsive" class="top">
    
    0 讨论(0)
  • 2020-11-22 08:35

    Yes, you can, you did it in the question itself: <html myAttri="myVal"/>.

    0 讨论(0)
  • 2020-11-22 08:35

    well! you can actually create a bunch of custom HTML attributes by disguising the data attributes in what you actually want.

    eg.

    [attribute='value']{
    color:red;
    }
    
    <span attribute="value" >hello world</span>
    

    It apparently works but that would invalidate your document, there is no need of using JScript for you to have custom attributes or even elements unless you have to, you just need to treat your new formulated(custom) attributes just the same way you treat your "data" attribute

    Remember "invalid" does not mean anything. The document will load fine at all the time. and some browsers would actually validate your document only by the presence of DOCTYPE....., you actually know what I mean.

    0 讨论(0)
  • 2020-11-22 08:35

    I can think of a handy use for the custom tag "init". Include a JavaScript expression that gets evaluated at document.onLoad() time and provides a value for the tag, e.g.

    <p>&lt;p&gt;The UTC date is &lt;span init="new Date().toUTCString()"&gt;?&lt;/span&gt;.&lt;p&gt;</p>
    

    Some boilerplate JavaScript code would scan all the tags in the DOM at document.onload() time looking for the init attributes, evaluating the expressions that they contain, and assigning them to the containing tag's innerHTML. This would give HTML some of the power of JSP, PHP etc. Currently we have to split the HTML markup and the JavaScript code that illuminates it. Bugs love split code.

    0 讨论(0)
  • 2020-11-22 08:38

    You can add custom attributes to your elements at will. But that will make your document invalid.

    In HTML 5 you will have the opportunity to use custom data attributes prefixed with data-.

    0 讨论(0)
  • 2020-11-22 08:40

    In HTML5: yes: use the data- attribute.

     <ul>
      <li data-animal-type="bird">Owl</li>
      <li data-animal-type="fish">Salmon</li>
      <li data-animal-type="spider">Tarantula</li>
    </ul> 
    
    0 讨论(0)
提交回复
热议问题