如何以有效的方式添加非标准属性
拥有非标准属性
onselectstart
oncontextmenu
...
有没有办法在标签中
,并且仍然以某种方式通过 HTML 4.01 过渡验证?除了稍后使用 Javascript 添加属性之外。
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?
Apart from adding the properties later on using Javascript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
虽然您无法在 HTML 4.01 中添加自己的标签或属性,但常见的技术是使用标准 HTML 标签或属性来包含您的信息,即使根据规范它并不完全正确。例如,“class”属性可以存储几乎任何类型的数据:
您可以检索上面链接的类并将“class”属性拆分以检索数据。
我见过用于自定义数据的其他一些标签和属性:带有非 JavaScript“type”值的
标签、隐藏的输入值、各种标签上的“title”属性。
如果您不介意从 HTML 4 进行更改,您还有其他几个选择:
您还可以通过 JavaScript 在运行时向 HTML 文档添加自定义属性。例如:
如果您的信息是动态的并且根本不需要存储在标记中,这会很有帮助。
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. For example, the 'class' attribute can store almost any kind of data:
You can retrieve the class of the link above and split the 'class' attribute up to retrieve your data.
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.You have a couple of other options if you don't mind changing from HTML 4:
You can also add custom attributes to your HTML document at runtime via JavaScript. For example:
This can be helpful if your information is dynamic and doesn't need to be stored in the markup at all.
使用这些属性将产生无效文档。
稍后使用 Javascript 添加这些属性将生成无效文档(即使 W3C 验证器无法告诉您这一点)。
但 W3C 从未反对使用专有扩展。验证不应成为一项要求。这是一种在您不符合规范时告诉您的方法。 W3C 不会仅仅因为无效页面而向 FBI 发送信息。
如果您依靠专有扩展来为访问者提供更好的体验(但不依赖它),那么您就走在了正确的道路上:-) 只是祈祷(或贡献)那些在下一个规范中的内容。
现在,如果它是为了阻止浏览器上下文菜单或选择,那就太粗鲁了!不要这样做!
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).
But W3C has never been against using proprietary extensions. 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.
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!
不,您必须更改文档类型。
该文档类型将允许您使用自己的属性。这是关于此事的一篇好文章
No, you would have to change the doctype.
That doctype will allow you to use your own attributes. 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.
我过去做过的一件事就是只使用“数据”,它通常用于幻灯片等。但是以与“样式”属性中相同的方式写出您需要的数据。
然后使用 jquery 来获取数据:
函数:
无效并不是世界末日,正如其他几个人提到的那样。正如我所说,“数据”很常用,甚至在某些 IDE 中突出显示为有效。
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.
Then use jquery to grab the data:
The function:
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.