How to add custom attributes in a real way

Is there a way to have non-standard attributes like

onselectstart
oncontextmenu 
...

in the tag and still being tested as HTML 4.01 transient somehow?

Other than adding properties later using Javascript.

+3
source share
5 answers

Using this attribute will result in an invalid document.

Adding these attributes later using Javascript will result in an invalid document (even if the W3C validator cannot tell you about it).

W3C . . , . W3C FBI .

, ( ), :-) ( ) , .


, , ! !

+5

HTML 4.01, HTML , . , "class" :

<a href="#" id="user-link-1" class="username:matt email:matt@example.com">Matt</a>

"class" .

, , : <script> "type", , "title" .

, HTML 4:

HTML- JavaScript. :

var body = document.getElementsByTagName("body")[0];
body["my-attribute"] = "Hello, world!";
alert(body["my-attribute"]);

, .

+5

, doctype.

<!DOCTYPE HTML>

doctype . Heres

+2

, . , .

+2
source

One thing that I have done in the past is to simply use the “data” that is commonly used in slide shows, etc. But write down the data you need in the same way as in the attribute "style".

<div class="your_element" data="onselectstart:foo;oncontextmenu:bar;">
</div>

Then use jquery to capture the data:

var onSelectStart = getData($(".your_element"), "onselectstart");

Function:

// 
function getData(elementObject, indexString)
{
    var dataElements = elementObject.attr("data").trim().split(";");
    var result = dataElements.some(function(entry) {
        dataArray = entry.trim().split(":");
        if(dataArray[0] == indexString)
        {
            found = dataArray[1].trim();
            return true; // return from "some"
        }
    });
    return result ? found : false;
}

Not really the end of the world, as several others have mentioned. As I said, "data" is usually used and even highlighted as valid in some IDEs.

0
source

Source: https://habr.com/ru/post/1723704/


All Articles