简体   繁体   English

如何以有效的方式添加非标准属性

[英]How to add non-standard attributes in a valid way

Is there a way to have non-standard attributes like 有没有办法让非标准的属性像

onselectstart
oncontextmenu 
...

in a tag, and still pass validation as HTML 4.01 transitional somehow? 在标签中,仍然以某种方式通过HTML 4.01过渡验证?

Apart from adding the properties later on using Javascript. 除了稍后使用Javascript添加属性。

Using those attribute will produce an invalid document. 使用这些属性将生成无效文档。

Adding those attributes later using Javascript will produce an invalid document (even if the W3C validator is unable to tell you so). 稍后使用Javascript添加这些属性将生成无效文档(即使W3C验证程序无法告诉您)。

But W3C has never been against using proprietary extensions. 但W3C从未反对使用专有扩展。 Validation should not be a requirement. 验证不应成为要求。 It is a way to tell you when you do not conform to the spec. 这是一种告诉你何时不符合规范的方法。 W3C will not send the FBI just for an invalid page. W3C不会仅针对无效页面发送FBI。

If you are relying on proprietary extensions to give your visitor a better experience (but not rely on it) then you are on the good path :-) Just pray (or contribute) for those to be in the next spec. 如果您依靠专有扩展来为您的访问者提供更好的体验(但不依赖于它),那么您就走上了良好的道路:-)只需为下一个规范中的那些人祈祷(或贡献)。


Now if it is about preventing browser context menu or selection, that's just rude! 现在,如果是关于阻止浏览器上下文菜单或选择,那就太粗鲁了! Don't do it! 不要这样做!

While you can't add your own tags or attributes in HTML 4.01, a common technique is using standard HTML tags or attributes to include your information, even if it isn't exactly the correct usage according to the spec. 虽然您无法在HTML 4.01中添加自己的标记或属性,但常用的技术是使用标准HTML标记或属性来包含您的信息,即使根据规范它不是完全正确的用法。 For example, the 'class' attribute can store almost any kind of data: 例如,'class'属性可以存储几乎任何类型的数据:

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

You can retrieve the class of the link above and split the 'class' attribute up to retrieve your data. 您可以检索上面链接的类并将“class”属性拆分为检索数据。

Some other tags and attributes I've seen used for custom data: <script> tags with a non-JavaScript 'type' value, hidden input values, the 'title' attribute on various tags. 我见过的一些其他标签和属性用于自定义数据: <script>标签带有非JavaScript“类型”值,隐藏输入值,各种标签上的“标题”属性。

You have a couple of other options if you don't mind changing from HTML 4: 如果您不介意从HTML 4更改,还有其他几个选项:

You can also add custom attributes to your HTML document at runtime via JavaScript. 您还可以在运行时通过JavaScript向HTML文档添加自定义属性。 For example: 例如:

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

This can be helpful if your information is dynamic and doesn't need to be stored in the markup at all. 如果您的信息是动态的,并且根本不需要存储在标记中,这将非常有用。

No, you would have to change the doctype. 不,您必须更改doctype。

<!DOCTYPE HTML>

That doctype will allow you to use your own attributes. 该doctype将允许您使用自己的属性。 Heres a good article on the matter 关于此事的一篇好文章

No, it isn't. 不,不是。 There is no scope for adding things to the language while still conforming to the language. 在符合语言的情况下,没有空间添加语言。

One thing I've done in the past is just use "data", which is commonly used in slideshows etc. But write out the data you need the same way you would in the "style" attribute. 我过去做过的一件事就是使用“数据”,它通常用于幻灯片等。但是要像在“样式”属性中那样写出所需的数据。

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

Then use jquery to grab the data: 然后使用jquery来获取数据:

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

The 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 being valid isn't the end of the world as a couple of others have mentioned. 不是有效的并不是世界末日,正如其他几个人所提到的那样。 Like I said, "data" is commonly used and it is even highlighted as valid in some IDEs. 就像我说的那样,“数据”是常用的,它甚至在某些IDE中突出显示为有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM