那么,如果自定义HTML属性不是有效的XHTML呢?

时间:2022-03-14 22:28:25

I know that is the reason some people don't approve of them, but does it really matter? I think that the power that they provide, in interacting with JavaScript and storing and sending information from and to the server, outweighs the validation concern. Am I missing something? What are the ramifications of "invalid" HTML? And wouldn't a custom DTD resolve them anyway?

我知道这是一些人不认同他们的原因,但这真的重要吗?我认为,在与JavaScript交互、存储和向服务器发送信息的过程中,它们所提供的功能超过了对验证的关注。我遗漏了什么东西?“无效”HTML的后果是什么?而自定义DTD又不会解决它们吗?

15 个解决方案

#1


76  

The ramification is that w3c comes along in 2, 5, 10 years and creates an attribute with the same name. Now your page is broken.

其分支是w3c在2,5,10年后出现,并创建具有相同名称的属性。现在你的页面坏了。

HTML5 is going to provide a data attribute type for legal custom attributes (like data-myattr="foo") so maybe you could start using that now and be reasonably safe from future name collisions.

HTML5将为合法的自定义属性(比如data-myattr="foo")提供一个数据属性类型,所以您现在就可以开始使用它了,并且在以后的名称冲突中也可以相当安全。

Finally, you may be overlooking that custom logic is the rational behind the class attribute. Although it is generally thought of as a style attribute it is in reality a legal way to set custom meta-properties on an element. Unfortunately you are basically limited to boolean properties which is why HTML5 is adding the data prefix.

最后,您可能忽略了自定义逻辑是类属性后面的rational。虽然它通常被认为是一个样式属性,但实际上它是在元素上设置自定义元属性的合法方法。不幸的是,您基本上仅限于布尔属性,这就是为什么HTML5要添加数据前缀。

BTW, by "basically boolean" I mean in principle. In reality there is nothing to stop you using a seperator in your class name to define custom values as well as attributes.

顺便说一句,我的意思是原则上是布尔式的。实际上,没有什么可以阻止您在类名中使用分配器来定义自定义值和属性。

class="document docId.56 permissions.RW"

docId类= "文档。56 permissions.RW”

#2


22  

Yes you can legally add custom attributes by using "data".

是的,您可以合法地使用“数据”添加自定义属性。

For example:

例如:

<div id="testDiv" data-myData="just testing"></div>

After that, just use the latest version of jquery to do something like:

之后,使用最新版本的jquery来完成如下操作:

alert($('#testDiv').data('myData'))

or to set a data attribute:

或设置数据属性:

$('#testDiv').data('myData', 'new custom data')

And since jQuery works in almost all browsers, you shouldn't have any problems ;)

由于jQuery几乎在所有浏览器中都能运行,所以您不应该有任何问题;

update

更新

  • data-myData may be converted to data-mydata in some browsers, as far as the javascript engine is concerned. Best to keep it lowercase all the way.
  • 就javascript引擎而言,在某些浏览器中,data-myData可以转换为mydata数据。最好一直保持小写。

#3


10  

Validation is not an end in itself, but a tool to be used to help catch mistakes early, and reduce the number of mysterious rendering and behavioural issues that your web pages may face when used on multiple browser types.

验证本身并不是目的,而是一种工具,用于帮助及早发现错误,并减少在多种浏览器中使用web页面时可能遇到的神秘的呈现和行为问题。

Adding custom attributes will not affect either of these issues now, and unlikely to do so in the future, but because they don't validate, it means that when you come to assess the output of a validation of your page, you will need to carefully pick between the validation issues that matter, and the ones that don't. Each time you change your page and revalidate, you have to repeat this operation. If your page validates entirely then you get a nice green PASS message, and you can move on the next stage of testing, or to the next change that needs to be made.

添加自定义属性不会影响这两个问题现在和将来不太可能这么做,而是因为他们不验证,这意味着当你评估的输出验证的页面,您需要仔细选择之间的验证问题,和那些不。每次更改页面并重新验证时,都必须重复此操作。如果您的页面完全验证,那么您将得到一个漂亮的绿色通行证消息,您可以继续进行下一个测试阶段,或者进行下一个需要进行的更改。

#4


8  

I've seen people obsessed with validation doing far worse/weird things than using a simple custom attribute:

我看到人们沉迷于验证比使用一个简单的自定义属性做的更糟糕/奇怪的事情:

<base href="http://example.com/" /><!--[if IE]></base><![endif]-->

In my opinion, custom attributes really don't matter. As other say, it may be good to watch out for future additions of attributes in the standards. But now we have data-* attributes in HTML5, so we're saved.

在我看来,定制属性真的不重要。正如其他人所说的那样,最好留心将来标准中添加的属性。但是现在我们有了HTML5中的数据-*属性,所以我们得救了。

What really matters is that you have properly nested tags, and properly quoted attribute values.

真正重要的是您有正确的嵌套标记和正确引用的属性值。

I even use custom tag names (those introduced by HTML5, like header, footer, etc), but these ones have problems in IE.

我甚至使用自定义标签名(HTML5引入的标签名,如header、footer等),但是这些标签在IE中存在问题。

By the way, I often find ironically how all those validation zealots bow in front of Google's clever tricks, like iframe uploads.

顺便说一句,我经常发现具有讽刺意味的是,这些验证*者是如何在谷歌的聪明把戏面前屈服的,比如iframe上传。

#5


7  

Instead of using custom attributes, you can associate your HTML elements with the attributes using JSON:

与使用自定义属性不同,您可以使用JSON将HTML元素与属性关联起来:

var customAttributes = { 'Id1': { 'custAttrib1': '', ... }, ... };

And as for the ramifications, see SpliFF's answer.

至于后果,请参阅斯普利夫的答案。

#6


5  

Storing multiple values in the class attribute is not correct code encapsulation and just a convoluted hack way of doing things. Take a custom ad rotator for instance that uses jquery. It is much cleaner on the page to do

在类属性中存储多个值并不是正确的代码封装,而是一种复杂的操作方法。以使用jquery的自定义广告旋转器为例。这页要干净得多

<div class="left blue imagerotator" AdsImagesDir="images/ads/" startWithImage="0" endWithImage="10" rotatorTimerSeconds="3" />

and let some simple jquery code do the work from here. Any developer or web designer now can work on the ad rotator and change values to this when asked without much ado.

让一些简单的jquery代码从这里开始工作。现在,任何开发人员或web设计人员都可以使用ad rotator,当有人要求时,可以毫不费力地更改它的值。

Coming back to project a year later or coming into a new one where the previous developer split and went to an island somewhere in the pacific can be hell trying to figure out intentions when code is written in an unclear encrypted manner like this:

一年后回到项目,或者进入一个新的项目,以前的开发人员分裂到太平洋上的某个岛屿上,当代码以这样一种不明确的加密方式编写时,可能会试图弄清楚意图:

<div class="left blue imagerotator dir:images-ads endwith:10 t:3 tf:yes"  />

When we write code in c# and other languages we don't write code putting all custom properties in one property as a space delimited string and end up having to parse that string every time we need to access or write to it. Think about the next person that will work on your code.

当我们用c#和其他语言编写代码时,我们不会编写将所有自定义属性作为空格分隔的字符串放入一个属性的代码,并且在每次需要访问或写入该字符串时都必须解析该字符串。考虑下一个要处理代码的人。

#7


2  

The thing with validation is that TODAY it may not matter, but you cannot know if it's going to matter tomorrow (and, by Murphy's law, it WILL matter tomorrow).

有验证的是,今天可能无关紧要,但你不知道明天是否会有影响(根据墨菲定律,明天会有影响)。

It's just better to choose a future-proof alternative. If they don't exist (they do in this particular case), the way to go is to invent a future proof alternative.

最好选择一种能够抵御未来的选择。如果他们不存在(他们在这个特定的例子中),那么去的方法就是发明一个未来的证明替代方案。

Using custom attributes is probably harmless, but still, why choose a potentially harmful solution just because you think (you can never be sure) it will cause no harm?. It might be worth to discuss this further if the future proof alternative was too costly or unwieldy, but this is certainly not the case.

使用自定义属性可能是无害的,但是,为什么仅仅因为您认为(您永远不能确定)它不会造成伤害而选择一个潜在的有害解决方案呢?如果未来的证据替代方案过于昂贵或笨拙,那么可能有必要进一步讨论这个问题,但肯定不是这样。

#8


2  

Old discussion but nevertheless; in my opinion since html is a mark-up and not a progamming language, it should always be interpreted with leniency for mark-up 'errors'. A browser is perfectly able to do so. I don't think this will and should change ever. Therefore, the only important practical criteria is that your html will be displayed correctly by most browsers and will continue to do so in, say a few years. After that time, your html will probalbly be redesigned anyway.

旧的讨论不过;在我看来,html是一种标记,而不是一种语言,它应该总是被宽大的解释为“错误”。浏览器完全可以做到这一点。我认为这种情况不会也不应该改变。因此,唯一重要的实用标准是,大多数浏览器将正确地显示html,并将在几年之内继续这样做。在那之后,你的html可能会被重新设计。

#9


2  

Just to add my ingredient to the mix, validation is also important when you need to create content that can/could be post-processed using automated tools. If your content is valid you can much more easily convert markup from one format to another. For example, doing valid XHTML to XML with a specific schema is Much easier when parsing data that you know and can verify to follow a predictable format.

为了将我的成分添加到混合中,当您需要创建可以/可以使用自动工具进行后处理的内容时,验证也很重要。如果您的内容是有效的,您可以更容易地将标记从一种格式转换为另一种格式。例如,使用特定模式对XML执行有效的XHTML时,解析您知道并可以验证遵循可预测格式的数据时要容易得多。

I, for example NEED my content to be valid XHTML because very often it is converted into XML for various jobs and then converted back without data loss or unexpected rendering results.

例如,我需要我的内容是有效的XHTML,因为它经常被转换为XML,用于不同的作业,然后在没有数据丢失或意外呈现结果的情况下被转换回来。

#10


1  

Well it depends on your client/boss/etc .. do they require it be validating XHTML?

这取决于你的客户/老板等。他们是否要求它验证XHTML?

Some people say there are a lot of workarounds - and depending on the sceneraio, they can work great. This includes adding classes, leveraging the rel attribute, and someone that has even written their own parser to extract JSON from HTML comments.

有些人说有很多变通的办法——根据场景的不同,它们可以很好地工作。这包括添加类、利用rel属性,以及甚至编写自己的解析器从HTML注释中提取JSON的人。

HTML5 provides a standard way to do this, prefix your custom attributes with "data-". I would recommend doing this now anyway, as there is a chance you may use an attribute that will be used down the track in standard XHTML.

HTML5提供了一种标准的方法,用“data-”对定制属性进行前缀。无论如何,我建议现在就这样做,因为您可能会使用在标准XHTML中使用的属性。

#11


0  

Using non-standard HTML could make the browser render the page in "quirks mode", in which case some other parts of the page may render differently, and other things like positioning may be slightly different. Using a custom DTD may get around this, though.

使用非标准的HTML可以使浏览器以“怪癖模式”呈现页面,在这种情况下,页面的其他部分可能呈现不同,而诸如定位之类的其他东西可能略有不同。不过,使用自定义DTD可以解决这个问题。

#12


0  

Because they're not standard you have no idea what might happen, neither now, nor in the future. As others have said W3C might start using those same names in the future. But what's even more dangerous is that you don't know what the developers of "browser xxx" have done when they encounter they.

因为它们不是标准的,你不知道会发生什么,无论是现在还是将来。正如其他人所说,W3C将来可能开始使用这些名称。但更危险的是,你不知道“浏览器xxx”的开发人员在遇到他们时做了什么。

Maybe the page is rendered in quirks mode, maybe the page doesn't render at all on some obscure mobile browser, maybe the browser will leak memory, maybe a virus killer will choke on your page, etc, etc, etc.

也许页面呈现的方式是怪怪的,也许页面在一些模糊的移动浏览器上根本不呈现,也许浏览器会泄漏内存,也许病毒杀手会阻塞你的页面,等等。

I know that following the standards religiously might seem like snobbery. However once you have experienced problems due to not following them, you tend to stop thinking like that. However, then it's mostly too late, and you need to start your application from scratch with a different framework...

我知道遵循宗教标准可能看起来很势利。然而,一旦你因为不遵循它们而经历了问题,你就会倾向于停止这样想。然而,现在一切都太迟了,您需要从头开始使用不同的框架……

#13


0  

I think developers validate just to validate, but there is something to be said for the fact that it keeps markup clean. However, because every (exaggeration warning!) browser displays everything differently there really is no standard. We try to follow standards because it makes us feel like we at least have some direction. Some people argue that keeping code standard will prevent issues and conflicts in the future. My opinion: Screw that nobody implements standards correctly and fully today anyway, might as well assume all your code will fail eventually. If it works it works, use it, unless its messy or your just trying to ignore standards to stick it to W3C or something. I think its important to remember that standards are implemented very slowly, has the web changed all that much in 5 years. I'm sure anyone will have years of notice when they need to fix a potential conflict. No reason to plan for compatibility of standards in the future when you can't even rely on today's standards.

我认为开发人员验证只是为了验证,但是有一点需要说明,它可以保持标记的整洁。然而,因为每个(夸张的警告!)浏览器显示的东西都不一样,所以真的没有标准。我们试图遵循标准,因为它让我们觉得我们至少有了一些方向。有些人认为,保持代码标准将防止将来的问题和冲突。我的观点是:不管怎样,现在没有人能够正确地、完整地实现标准,这也可能意味着您的所有代码最终都会失败。如果它是可行的,那么就使用它,除非它很乱,或者你只是试图忽略标准,把它贴在W3C或其他什么东西上。我认为重要的是要记住,标准的实施是非常缓慢的,在5年的时间里,网络改变了那么多。我敢肯定,任何需要解决潜在冲突的人都会有好几年的时间。当你甚至不能依赖于今天的标准的时候,没有理由去计划未来的标准兼容性。

Oh I almost forgot, if your code doesn't validate 10 baby kittens will die. Are you a kitten killer?

哦,我差点忘了,如果你的代码没有验证10只小猫会死。你是小猫杀手吗?

#14


0  

Jquery .html(markup) doesn't work if markup is invalid.

如果标记无效,则Jquery .html(标记)不起作用。

#15


0  

Validation

You shouldn't need custom attributes to provide validation. A better approach would be to add validation based on fields actual task.

您不应该需要自定义属性来提供验证。一个更好的方法是在字段实际任务的基础上添加验证。

Assign meaning by using classes. I have classnames like:

通过使用类来分配意义。我有类名:

  • date (Dates)
  • 日期(日期)
  • zip (Zip code)
  • 邮政编码(邮政编码)
  • area (Areas)
  • 区域(区域)
  • ssn (Social security number)
  • ssn(社会安全号码)

Example markup:

示例标记:

<input class="date" name="date" value="2011-08-09" />

Example javascript (with jQuery):

与jQuery示例javascript():

$('.date').validate(); // use your custom function/framework etc here.

If you need special validators for a certain or scenario you just invent new classes (or use selectors) for your special case:

如果您需要为特定的或场景创建特殊的验证器,您只需为您的特殊情况创建新的类(或使用选择器):

Example for checking if two passwords match:

检查两个密码是否匹配的示例:

<input id="password" />
<input id="password-confirm" />

if($('#password').val() != $('#password-confirm').val())
{
 // do something if the passwords don't match
}

(This approach works quite seamless with both jQuery validation and the mvc .net framework and probably others too)

(这种方法在jQuery验证和mvc。net框架以及其他框架中都可以无缝地工作)

Bonus: You can assign multiple classes separated with a space class="ssn custom-one custom-two"

额外的好处:您可以分配多个与空间类分离的类=“ssn定制- 1定制- 2”

Sending information "from and to the server"

If you need to pass data back, use <input type="hidden" />. They work out of the box.

如果需要将数据传回,请使用。他们在盒子里工作。

(Make sure you don't pass any sensitive data with hidden inputs since they can be modified by the user with almost no effort at all)

(确保您不会传递任何带有隐藏输入的敏感数据,因为用户几乎不需要花费任何力气就可以修改这些数据)

#1


76  

The ramification is that w3c comes along in 2, 5, 10 years and creates an attribute with the same name. Now your page is broken.

其分支是w3c在2,5,10年后出现,并创建具有相同名称的属性。现在你的页面坏了。

HTML5 is going to provide a data attribute type for legal custom attributes (like data-myattr="foo") so maybe you could start using that now and be reasonably safe from future name collisions.

HTML5将为合法的自定义属性(比如data-myattr="foo")提供一个数据属性类型,所以您现在就可以开始使用它了,并且在以后的名称冲突中也可以相当安全。

Finally, you may be overlooking that custom logic is the rational behind the class attribute. Although it is generally thought of as a style attribute it is in reality a legal way to set custom meta-properties on an element. Unfortunately you are basically limited to boolean properties which is why HTML5 is adding the data prefix.

最后,您可能忽略了自定义逻辑是类属性后面的rational。虽然它通常被认为是一个样式属性,但实际上它是在元素上设置自定义元属性的合法方法。不幸的是,您基本上仅限于布尔属性,这就是为什么HTML5要添加数据前缀。

BTW, by "basically boolean" I mean in principle. In reality there is nothing to stop you using a seperator in your class name to define custom values as well as attributes.

顺便说一句,我的意思是原则上是布尔式的。实际上,没有什么可以阻止您在类名中使用分配器来定义自定义值和属性。

class="document docId.56 permissions.RW"

docId类= "文档。56 permissions.RW”

#2


22  

Yes you can legally add custom attributes by using "data".

是的,您可以合法地使用“数据”添加自定义属性。

For example:

例如:

<div id="testDiv" data-myData="just testing"></div>

After that, just use the latest version of jquery to do something like:

之后,使用最新版本的jquery来完成如下操作:

alert($('#testDiv').data('myData'))

or to set a data attribute:

或设置数据属性:

$('#testDiv').data('myData', 'new custom data')

And since jQuery works in almost all browsers, you shouldn't have any problems ;)

由于jQuery几乎在所有浏览器中都能运行,所以您不应该有任何问题;

update

更新

  • data-myData may be converted to data-mydata in some browsers, as far as the javascript engine is concerned. Best to keep it lowercase all the way.
  • 就javascript引擎而言,在某些浏览器中,data-myData可以转换为mydata数据。最好一直保持小写。

#3


10  

Validation is not an end in itself, but a tool to be used to help catch mistakes early, and reduce the number of mysterious rendering and behavioural issues that your web pages may face when used on multiple browser types.

验证本身并不是目的,而是一种工具,用于帮助及早发现错误,并减少在多种浏览器中使用web页面时可能遇到的神秘的呈现和行为问题。

Adding custom attributes will not affect either of these issues now, and unlikely to do so in the future, but because they don't validate, it means that when you come to assess the output of a validation of your page, you will need to carefully pick between the validation issues that matter, and the ones that don't. Each time you change your page and revalidate, you have to repeat this operation. If your page validates entirely then you get a nice green PASS message, and you can move on the next stage of testing, or to the next change that needs to be made.

添加自定义属性不会影响这两个问题现在和将来不太可能这么做,而是因为他们不验证,这意味着当你评估的输出验证的页面,您需要仔细选择之间的验证问题,和那些不。每次更改页面并重新验证时,都必须重复此操作。如果您的页面完全验证,那么您将得到一个漂亮的绿色通行证消息,您可以继续进行下一个测试阶段,或者进行下一个需要进行的更改。

#4


8  

I've seen people obsessed with validation doing far worse/weird things than using a simple custom attribute:

我看到人们沉迷于验证比使用一个简单的自定义属性做的更糟糕/奇怪的事情:

<base href="http://example.com/" /><!--[if IE]></base><![endif]-->

In my opinion, custom attributes really don't matter. As other say, it may be good to watch out for future additions of attributes in the standards. But now we have data-* attributes in HTML5, so we're saved.

在我看来,定制属性真的不重要。正如其他人所说的那样,最好留心将来标准中添加的属性。但是现在我们有了HTML5中的数据-*属性,所以我们得救了。

What really matters is that you have properly nested tags, and properly quoted attribute values.

真正重要的是您有正确的嵌套标记和正确引用的属性值。

I even use custom tag names (those introduced by HTML5, like header, footer, etc), but these ones have problems in IE.

我甚至使用自定义标签名(HTML5引入的标签名,如header、footer等),但是这些标签在IE中存在问题。

By the way, I often find ironically how all those validation zealots bow in front of Google's clever tricks, like iframe uploads.

顺便说一句,我经常发现具有讽刺意味的是,这些验证*者是如何在谷歌的聪明把戏面前屈服的,比如iframe上传。

#5


7  

Instead of using custom attributes, you can associate your HTML elements with the attributes using JSON:

与使用自定义属性不同,您可以使用JSON将HTML元素与属性关联起来:

var customAttributes = { 'Id1': { 'custAttrib1': '', ... }, ... };

And as for the ramifications, see SpliFF's answer.

至于后果,请参阅斯普利夫的答案。

#6


5  

Storing multiple values in the class attribute is not correct code encapsulation and just a convoluted hack way of doing things. Take a custom ad rotator for instance that uses jquery. It is much cleaner on the page to do

在类属性中存储多个值并不是正确的代码封装,而是一种复杂的操作方法。以使用jquery的自定义广告旋转器为例。这页要干净得多

<div class="left blue imagerotator" AdsImagesDir="images/ads/" startWithImage="0" endWithImage="10" rotatorTimerSeconds="3" />

and let some simple jquery code do the work from here. Any developer or web designer now can work on the ad rotator and change values to this when asked without much ado.

让一些简单的jquery代码从这里开始工作。现在,任何开发人员或web设计人员都可以使用ad rotator,当有人要求时,可以毫不费力地更改它的值。

Coming back to project a year later or coming into a new one where the previous developer split and went to an island somewhere in the pacific can be hell trying to figure out intentions when code is written in an unclear encrypted manner like this:

一年后回到项目,或者进入一个新的项目,以前的开发人员分裂到太平洋上的某个岛屿上,当代码以这样一种不明确的加密方式编写时,可能会试图弄清楚意图:

<div class="left blue imagerotator dir:images-ads endwith:10 t:3 tf:yes"  />

When we write code in c# and other languages we don't write code putting all custom properties in one property as a space delimited string and end up having to parse that string every time we need to access or write to it. Think about the next person that will work on your code.

当我们用c#和其他语言编写代码时,我们不会编写将所有自定义属性作为空格分隔的字符串放入一个属性的代码,并且在每次需要访问或写入该字符串时都必须解析该字符串。考虑下一个要处理代码的人。

#7


2  

The thing with validation is that TODAY it may not matter, but you cannot know if it's going to matter tomorrow (and, by Murphy's law, it WILL matter tomorrow).

有验证的是,今天可能无关紧要,但你不知道明天是否会有影响(根据墨菲定律,明天会有影响)。

It's just better to choose a future-proof alternative. If they don't exist (they do in this particular case), the way to go is to invent a future proof alternative.

最好选择一种能够抵御未来的选择。如果他们不存在(他们在这个特定的例子中),那么去的方法就是发明一个未来的证明替代方案。

Using custom attributes is probably harmless, but still, why choose a potentially harmful solution just because you think (you can never be sure) it will cause no harm?. It might be worth to discuss this further if the future proof alternative was too costly or unwieldy, but this is certainly not the case.

使用自定义属性可能是无害的,但是,为什么仅仅因为您认为(您永远不能确定)它不会造成伤害而选择一个潜在的有害解决方案呢?如果未来的证据替代方案过于昂贵或笨拙,那么可能有必要进一步讨论这个问题,但肯定不是这样。

#8


2  

Old discussion but nevertheless; in my opinion since html is a mark-up and not a progamming language, it should always be interpreted with leniency for mark-up 'errors'. A browser is perfectly able to do so. I don't think this will and should change ever. Therefore, the only important practical criteria is that your html will be displayed correctly by most browsers and will continue to do so in, say a few years. After that time, your html will probalbly be redesigned anyway.

旧的讨论不过;在我看来,html是一种标记,而不是一种语言,它应该总是被宽大的解释为“错误”。浏览器完全可以做到这一点。我认为这种情况不会也不应该改变。因此,唯一重要的实用标准是,大多数浏览器将正确地显示html,并将在几年之内继续这样做。在那之后,你的html可能会被重新设计。

#9


2  

Just to add my ingredient to the mix, validation is also important when you need to create content that can/could be post-processed using automated tools. If your content is valid you can much more easily convert markup from one format to another. For example, doing valid XHTML to XML with a specific schema is Much easier when parsing data that you know and can verify to follow a predictable format.

为了将我的成分添加到混合中,当您需要创建可以/可以使用自动工具进行后处理的内容时,验证也很重要。如果您的内容是有效的,您可以更容易地将标记从一种格式转换为另一种格式。例如,使用特定模式对XML执行有效的XHTML时,解析您知道并可以验证遵循可预测格式的数据时要容易得多。

I, for example NEED my content to be valid XHTML because very often it is converted into XML for various jobs and then converted back without data loss or unexpected rendering results.

例如,我需要我的内容是有效的XHTML,因为它经常被转换为XML,用于不同的作业,然后在没有数据丢失或意外呈现结果的情况下被转换回来。

#10


1  

Well it depends on your client/boss/etc .. do they require it be validating XHTML?

这取决于你的客户/老板等。他们是否要求它验证XHTML?

Some people say there are a lot of workarounds - and depending on the sceneraio, they can work great. This includes adding classes, leveraging the rel attribute, and someone that has even written their own parser to extract JSON from HTML comments.

有些人说有很多变通的办法——根据场景的不同,它们可以很好地工作。这包括添加类、利用rel属性,以及甚至编写自己的解析器从HTML注释中提取JSON的人。

HTML5 provides a standard way to do this, prefix your custom attributes with "data-". I would recommend doing this now anyway, as there is a chance you may use an attribute that will be used down the track in standard XHTML.

HTML5提供了一种标准的方法,用“data-”对定制属性进行前缀。无论如何,我建议现在就这样做,因为您可能会使用在标准XHTML中使用的属性。

#11


0  

Using non-standard HTML could make the browser render the page in "quirks mode", in which case some other parts of the page may render differently, and other things like positioning may be slightly different. Using a custom DTD may get around this, though.

使用非标准的HTML可以使浏览器以“怪癖模式”呈现页面,在这种情况下,页面的其他部分可能呈现不同,而诸如定位之类的其他东西可能略有不同。不过,使用自定义DTD可以解决这个问题。

#12


0  

Because they're not standard you have no idea what might happen, neither now, nor in the future. As others have said W3C might start using those same names in the future. But what's even more dangerous is that you don't know what the developers of "browser xxx" have done when they encounter they.

因为它们不是标准的,你不知道会发生什么,无论是现在还是将来。正如其他人所说,W3C将来可能开始使用这些名称。但更危险的是,你不知道“浏览器xxx”的开发人员在遇到他们时做了什么。

Maybe the page is rendered in quirks mode, maybe the page doesn't render at all on some obscure mobile browser, maybe the browser will leak memory, maybe a virus killer will choke on your page, etc, etc, etc.

也许页面呈现的方式是怪怪的,也许页面在一些模糊的移动浏览器上根本不呈现,也许浏览器会泄漏内存,也许病毒杀手会阻塞你的页面,等等。

I know that following the standards religiously might seem like snobbery. However once you have experienced problems due to not following them, you tend to stop thinking like that. However, then it's mostly too late, and you need to start your application from scratch with a different framework...

我知道遵循宗教标准可能看起来很势利。然而,一旦你因为不遵循它们而经历了问题,你就会倾向于停止这样想。然而,现在一切都太迟了,您需要从头开始使用不同的框架……

#13


0  

I think developers validate just to validate, but there is something to be said for the fact that it keeps markup clean. However, because every (exaggeration warning!) browser displays everything differently there really is no standard. We try to follow standards because it makes us feel like we at least have some direction. Some people argue that keeping code standard will prevent issues and conflicts in the future. My opinion: Screw that nobody implements standards correctly and fully today anyway, might as well assume all your code will fail eventually. If it works it works, use it, unless its messy or your just trying to ignore standards to stick it to W3C or something. I think its important to remember that standards are implemented very slowly, has the web changed all that much in 5 years. I'm sure anyone will have years of notice when they need to fix a potential conflict. No reason to plan for compatibility of standards in the future when you can't even rely on today's standards.

我认为开发人员验证只是为了验证,但是有一点需要说明,它可以保持标记的整洁。然而,因为每个(夸张的警告!)浏览器显示的东西都不一样,所以真的没有标准。我们试图遵循标准,因为它让我们觉得我们至少有了一些方向。有些人认为,保持代码标准将防止将来的问题和冲突。我的观点是:不管怎样,现在没有人能够正确地、完整地实现标准,这也可能意味着您的所有代码最终都会失败。如果它是可行的,那么就使用它,除非它很乱,或者你只是试图忽略标准,把它贴在W3C或其他什么东西上。我认为重要的是要记住,标准的实施是非常缓慢的,在5年的时间里,网络改变了那么多。我敢肯定,任何需要解决潜在冲突的人都会有好几年的时间。当你甚至不能依赖于今天的标准的时候,没有理由去计划未来的标准兼容性。

Oh I almost forgot, if your code doesn't validate 10 baby kittens will die. Are you a kitten killer?

哦,我差点忘了,如果你的代码没有验证10只小猫会死。你是小猫杀手吗?

#14


0  

Jquery .html(markup) doesn't work if markup is invalid.

如果标记无效,则Jquery .html(标记)不起作用。

#15


0  

Validation

You shouldn't need custom attributes to provide validation. A better approach would be to add validation based on fields actual task.

您不应该需要自定义属性来提供验证。一个更好的方法是在字段实际任务的基础上添加验证。

Assign meaning by using classes. I have classnames like:

通过使用类来分配意义。我有类名:

  • date (Dates)
  • 日期(日期)
  • zip (Zip code)
  • 邮政编码(邮政编码)
  • area (Areas)
  • 区域(区域)
  • ssn (Social security number)
  • ssn(社会安全号码)

Example markup:

示例标记:

<input class="date" name="date" value="2011-08-09" />

Example javascript (with jQuery):

与jQuery示例javascript():

$('.date').validate(); // use your custom function/framework etc here.

If you need special validators for a certain or scenario you just invent new classes (or use selectors) for your special case:

如果您需要为特定的或场景创建特殊的验证器,您只需为您的特殊情况创建新的类(或使用选择器):

Example for checking if two passwords match:

检查两个密码是否匹配的示例:

<input id="password" />
<input id="password-confirm" />

if($('#password').val() != $('#password-confirm').val())
{
 // do something if the passwords don't match
}

(This approach works quite seamless with both jQuery validation and the mvc .net framework and probably others too)

(这种方法在jQuery验证和mvc。net框架以及其他框架中都可以无缝地工作)

Bonus: You can assign multiple classes separated with a space class="ssn custom-one custom-two"

额外的好处:您可以分配多个与空间类分离的类=“ssn定制- 1定制- 2”

Sending information "from and to the server"

If you need to pass data back, use <input type="hidden" />. They work out of the box.

如果需要将数据传回,请使用。他们在盒子里工作。

(Make sure you don't pass any sensitive data with hidden inputs since they can be modified by the user with almost no effort at all)

(确保您不会传递任何带有隐藏输入的敏感数据,因为用户几乎不需要花费任何力气就可以修改这些数据)