如何在选择时使 HTML 单选按钮变为粗体?

2022-01-21 00:00:00 radio-button html

在 HTML 中,我有以下单选按钮选项

In HTML, I have the following radio button choices

o 选择 1
o 选择 2
o 选择 3

o Choice 1
o Choice 2
o Choice 3

我想要做的是,当有人从上方选择一个单选按钮时,与所选单选按钮对应的文本变为粗体.

What I would like to do is when someone SELECTs a radio button from above, that the text corresponding to that radio button selected becomes bold.

因此,例如 - 如果该人选择选择 2",则单选按钮选择现在看起来像:

So, for example - if the person selects "Choice 2", the radio button selection would now look like:

o 选择 1
o 选择 2
o 选择 3

o Choice 1
o Choice 2
o Choice 3

我该怎么做?我假设必须使用 JavaScript 或 CSS.

How do I do this? I assume either JavaScript or CSS has to be used.

提前致谢

更新:如果不使用 jQuery,你将如何实现nickf"解决方案?

推荐答案

你可以纯粹用 CSS 来做到这一点,但你需要将每个输入的文本包装在 span 或其他标签中:

You can do this purely with CSS, but you need to wrap the text of each input in a span or other tag:

<input type="radio" name="group1" value="Milk"><span>Milk</span><br>

然后只使用兄弟选择器:

Then just use the sibling selector:

input[type="radio"]:checked+span { font-weight: bold; }

相关文章