html 怎么设置单选框的样式

html-css09

html 怎么设置单选框的样式,第1张

1、首先打开编辑器,然后新建一个html文件,编写入基本的框架。

2、然后用form,input和label创建一个单项选择题。

3、创建一个新的css文件,并且用link标签关联HTML文件。

4、然后撤销一下原来按钮的样式。

nput[type="radio"] {

  display: none

}

5、nput[type='radio'] + label:before{

  content: ""

  display: inline-block

  width: 20px

  height: 20px

  border: 2px solid gold

}

首先设置未点击的单选框样式,用border来设置颜色。

6、nput[type='radio']:checked + label:before{

  background-color: red

}

接着设置点击后的背景颜色,这里用background-color。

7、最后运行一下,完成效果图。

方法有两种。

第一种通过<select>的属性来设置选中项,此方法可以在动态语言如php在后台根据需要控制输出结果。

<select  id =  "sel" >

<option  value = "1" >1</ option >

<option  value = "2"  selected = "selected" >2</ option >

<option  value = "3" >3</ option >

</ select > 

第二种为通过前端js来控制选中的项:

<script  type = "text/javascript" >

function change(){

document.getElementById("sel")[2].selected=true

}

</ script >

<select  id =  "sel" >

<option  value = "1" >1</ option >

<option  value = "2" >2</ option >

<option  value = "3" >3</ option >

</ select >

<input  type = "button"  value = "修改"  onclick = "change()"  />

获取<select>标签选中项文本的js代码为:

var  val = document.all.Item.options[document.all.Item.selectedIndex].text

var  i=document.getElementById('sel').options[document.getElementById('sel').selectedIndex].value

扩展资料

Radio 对象代表 HTML 表单中的单选按钮。在 HTML 表单中 <input type="radio">每出现一次,一个 Radio 对象就会被创建。

单选按钮是表示一组互斥选项按钮中的一个。当一个按钮被选中,之前选中的按钮就变为非选中的。当单选按钮被选中或不选中时,该按钮就会触发 onclick 事件句柄。您可通过遍历表单的 elements[] 数组来访问 Radio 对象,或者通过使用 document.getElementById()。

参考资料:百度百科-radio