【问题标题】:how to set a radio button state to checked with css?如何设置单选按钮状态以使用 css 检查?
【发布时间】:2013-02-26 00:45:23
【问题描述】:

我有一组单选按钮,默认选中一个。像这样:

    <input type="radio" name="radioButton" id="rd1" checked="">
    <input type="radio" name="radioButton" id="rd2">
    <input type="radio" name="radioButton" id="rd3">
    <input type="radio" name="radioButton" id="rd4">
    <input type="radio" name="radioButton" id="rd5">

我知道有办法使用 javascript 添加选中的属性。我的问题是如何使用 css 将选中的属性添加到选定的单选按钮?

【问题讨论】:

  • CSS 顾名思义就是用于样式化。 DOM 操作应该使用 Javascript 来完成。

标签: html css radio-button


【解决方案1】:

如果您想更改最初选中的框,您只需将“checked”属性移动到 HTML 中的所需元素,如果您想设置当前选中框的样式,您可以使用以下 CSS 选择器:input[type=checkbox]:checked如果您想通过单击以外的任何其他操作更改选中状态必须使用 JS 的复选框。

“checked”属性与 JS 中实际的 .checked 属性不同:而 elem.getAttribute('checked') 只会返回 null 以外的其他属性,前提是您的 HTML 中已经设置了该属性(也可以通过 JS 更改),@ 987654325@ 将始终返回一个布尔值 (true/false),指示当前是否选中此框。

【讨论】:

    猜你喜欢
    • 2012-03-17
    • 1970-01-01
    • 2013-09-20
    • 2011-04-28
    • 2013-05-22
    • 2018-07-23
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    相关资源
    最近更新 更多