What is the “generated” attribute seen in some HTML tag used for?

前端 未结 2 1515
走了就别回头了
走了就别回头了 2021-01-11 18:13

I saw it used in a HTML label tag but I have a feeling that it can be used with most HTML tags. I can kinda guess what it means. But I am more curious about what\'s the bene

2条回答
  •  再見小時候
    2021-01-11 18:31

    You could use it as a hook for JavaScript and/or CSS.

    For example...

    CSS

    label[generated=true] {
       color: #ccc;
    }
    

    JavaScript

    var labels = document.getElementsByTagName('label');
    
    for (var i = 0, length = labels.length; i < length; i++) {
        if (labels[i].getAttribute('generated') === 'true') {
           // Do something.
        }
    }
    

    It is often used on JavaScript generated elements, such as the label elements created by the jQuery Validation plugin.

    You could use jQuery to clean up generated elements with...

    $('*[generated=true]').remove();
    

提交回复
热议问题