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

问题描述

目前在我们的插件中,我们通过设置复选框来设置

 < input type =checkboxchecked = checked/> 

这是为了保持xhtml的兼容性。我更习惯于将检查设置为属性

 < input type =checkboxchecked /> 

在html5中正确的处理方式是什么?我们是否应该关心xhtml的兼容性?

解决方案

这是两种情况下的属性。在任何一种情况下,它都会在元素节点的DOM属性上设置一个值(相同的值, true )。



checked =checked。
  • 在样式中,使用属性选择器时,语法并不完全相同(较短的形式不匹配 [checked = checked] ),但这在实践中并不重要:无论哪种情况, [checked] li>
  • 笨拙的语法 checked =checked是来自SGML的保留并且仅用于兼容性,所以它可能会使您的代码看起来老旧 - (很少有问题)。


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

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

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

    <input type="checkbox" checked />
    

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

    解决方案

    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:

    • 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