【发布时间】:2017-11-11 13:53:13
【问题描述】:
我正在尝试创建一个 JavaScript 函数,该函数允许我们根据我们的选择更改 div 的边框颜色。如果用户选择 Div 1 和 Gray 1,则 Div 1 的边框颜色将在点击Make Change 后变为color:#333333;。
HTML:
<div id="div_6">
Div Selector:
<!-- Place Div Select Here -->
<select>
<option>Div 1</option>
<option>Div 2</option>
<option>Div 3</option>
</select>
<br /><br />
Color Selector:
<!-- Place Color Select Here -->
<select>
<option>Gray 1</option>
<option>Gray 2</option>
<option>Gray 3</option>
</select>
<!-- Place "Change Color" Button Here -->
<button>Change Color</button>
</div>
CSS:
/*color selectors*/
#Gray1 {
color:#333333;
}
#Gray2 {
color:#777777;
}
#Gray3 {
color:#CCCCCC;
}
所以函数需要访问这些 ID。请在此处查看整个代码/网站:
创建此函数时,我什至不知道从哪里开始。我是否需要为 HTML 中的各种类分配选项?谢谢!
编辑当然,脚本会放在<head> 的<script> 部分:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="This is a simple CSS, HTML, and JS Test"/>
<title>WebToMed Website Developer Test</title>
<style>
/* Add all CSS enteries here */
#div_1 {
background: red;
display: table-cell;
text-align: left;
width: 300px;
height: 50px;
}
#div_2 {
background: green;
display: table-cell;
text-align: center;
height: 50px;
}
#div_3 {
background: blue;
display: table-cell;
text-align: right;
width: 350px;
color: #ffffff;
font-weight: bold;
text-decoration: underline;
height: 50px;
}
#div_4 {
background: yellow;
display: flex;
text-align: center;
height: 300px;
}
#div_5 {
margin-top: auto;
margin-bottom: auto;
margin-right: auto;
margin-left: auto;
}
#div_6 {
border: 3px solid black;
margin-top: 10px;
padding: 20px;
}
.container {
display: table;
width: 100%;
}
/*color selectors*/
#Gray1 {
color:#333333;
}
#Gray2 {
color:#777777;
}
#Gray3 {
color:#CCCCCC;
}
</style>
<script>
/* Add any JavaScript here */
</script>
</head>
<body>
【问题讨论】:
-
jsfiddle.net/yt2u8z3w 这是一个适合你的小提琴;)
-
@mr.void 您为什么不将其粘贴为答案?
-
我的英语不好解释解决方案:(
标签: javascript jquery html css