本文介绍了在html5中将复选框设置为选中状态时,我应该简单地使用checked(作为属性)还是用checked ="checked"(作为属性)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,在我们的插件中,我们将复选框设置为已选中

Currently in our plugin we were setting the checkboxes as checked by setting

<input type="checkbox" checked="checked" />

这是为了保留xhtml的兼容性.我更习惯将checked设置为属性

This was to preserve xhtml compatibility. I'm more used to setting checked as a property

<input type="checkbox" checked />

使用html5进行处理的正确方法是什么?我们仍然应该关心xhtml的兼容性吗?

What is the correct way to proceed in html5?Should we still care about xhtml compatibility?

推荐答案

无论哪种情况,它都是一个属性.不论哪种情况,它都会在元素节点的DOM属性上设置一个值(相同的值, true ).

It is an attribute in either case. And it sets a value (the same value, true) on a DOM property of the element node in either case.

在大多数情况下,使用哪种语法都没有关系.但是,有一些注意事项:

For most purposes, it does not matter which syntax you use. However, there are some points to note:

  • 如果在XML序列化中使用HTML5("XHTML5"),则必须使用 checked ="checked" .
  • 在样式中,使用属性选择器时语法并不完全相同(较短的形式与 [checked = checked] 不匹配"),但实际上并不重要: [checked] 在两种情况下都匹配选中的复选框.
  • 笨拙的语法 checked ="checked" 是SGML的保留,仅出于兼容性考虑而包含在内,因此它可能会使您的代码显得过时(这很少有关系).
  • If you use HTML5 in XML serialization ("XHTML5"), you must use checked="checked".
  • In styling, the syntaxes are not quite equivalent when using attribute selectors (the shorter form does not match [checked=checked]), but this does not matter in practice: [checked] matches checked checkboxes in either case.
  • The clumsy syntax checked="checked" is a holdover from SGML and included for compatibility only, so it may make your code look old-fashioned (which rarely matters).

这篇关于在html5中将复选框设置为选中状态时,我应该简单地使用checked(作为属性)还是用checked ="checked"(作为属性)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:57