使用单选按钮更改背景颜色 [英] Change background color using radio button

查看:133
本文介绍了使用单选按钮更改背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个菜鸟,

使用单选按钮更改背景颜色



我尝试了什么:



im a noob,
change background color using radio button

What I have tried:

<html>
<head>
<script language="javascript">
function changeclr()
{
	var col=document.getElementById('ccolor').value;
	documnet:body.backgroundColor='col';
}
</script>
</head>
<body>
<form>
<input type="radio" name="ccolor" value="red" onclick="changeclr()"> Red<br>
<input type="radio" name="ccolor" value="blue" onclick="changeclr()"> Blue<br>
<input type="radio" name="ccolor" value="white" onclick="changeclr()"> White<br>
<form>
</body>
</html>

推荐答案

冷杉:你不要声明ID =ccolor,



第二:你不能为每3个输入标签声明相同的ID,你必须在你的函数中声明class =ccolor:

Firs: You Do not declare ID="ccolor",

Second: You Can NOT declare same ID for every 3 input tags, You have to declare class="ccolor" and in your function:
var col = document.getElementByClassName("ccolor")[0].value;
document.body.BackgroundColor = col;


对您的函数进行了一些小改动,并传递了额外的输入参数。

A small change to your function and additional input param passed.
<html>
<head>
<script language="javascript">
function changeclr(obj)
{
	document.body.style.backgroundColor=obj.value;
}
</script>
</head>
<body>
<form>
<input type="radio" name="ccolor" value="red" onclick="changeclr(this)"> Red<br>
<input type="radio" name="ccolor" value="blue" onclick="changeclr(this)"> Blue<br>
<input type="radio" name="ccolor" value="white" onclick="changeclr(this)"> White<br>
<form>
</body>
</html>


这篇关于使用单选按钮更改背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆