我试图让 div 在选择表单选择菜单时切换样式属性。任何帮助都会很棒!
有代码:
选择标签(我有脚本函数的值):
<div style="float: right; margin-right: 5%; width: auto;">
<select style="border: 3px intset; border-radius: 5px; border-color: #17FFFF;" onchange="showstuff(this.value);">
<optgroup label="Lisboa">
<option value="Picoas">Picoas</option>
<option value="Benfica">Benfica</option>
</optgroup>
<optgroup label="Porto">
<option value="Felgueiras">Felgueiras</option>
<option value="Maia">Maia</option>
</optgroup>
</select>
</div>
带有类名的div:
<div style="width: 90%; height: 50%; background-color: #09C; overflow: scroll; overflow-x: hidden; margin-bottom: 15%; margin-left: 5%; margin-right: 5%; text-align: left; color: #000; font-size: 60%;">
<div class="Picoas" style="height: 30%; width: 100%; background-color: #CCEAFF; display:none;">
Timberland Picoas<br />
Centro Colombo Morada: Av. Lusíada, Centro Colombo Piso 1, Loja 1.095<br />
1500-392 Lisboa
</div>
<div class="Felgueiras" style="height: 30%; width: 100%; background-color: #CCEAFF;
margin-top: 2%;display:none;">
Timberland Felgueiras<br />
Centro Colombo Morada: Av. Lusíada, Centro Colombo Piso 1, Loja 1.095<br />
1500-392 Lisboa
</div>
<div class="Picoas" style="height: 30%; width: 100%; background-color: #CCEAFF; margin-top: 2%;display:none;">
Timberland Picoas<br />
Centro Colombo Morada: Av. Lusíada, Centro Colombo Piso 1, Loja 1.095<br />
1500-392 Lisboa
</div>
<div class="Felgueiras" style="height: 30%; width: 100%; background-color: #CCEAFF;
margin-top: 2%;display:none;">
Timberland Felgueiras<br />
Centro Colombo Morada: Av. Lusíada, Centro Colombo Piso 1, Loja 1.095<br />
1500-392 Lisboa
</div>
<div class="Benfica" style="height: 30%; width: 100%; background-color: #CCEAFF; margin-top: 2%;display:none;">
Timberland Benfica<br />
Centro Colombo Morada: Av. Lusíada, Centro Colombo Piso 1, Loja 1.095<br />
1500-392 Lisboa
</div>
<div class="Maia" style="height: 30%; width: 100%; background-color: #CCEAFF; margin-top: 2%;display:none;">
Timberland Maia<br />
Centro Colombo Morada: Av. Lusíada, Centro Colombo Piso 1, Loja 1.095<br />
1500-392 Lisboa
</div>
<div class="Benfica" style="height: 30%; width: 100%; background-color: #CCEAFF; margin-top: 2%;display:none;">
Timberland Benfica<br />
Centro Colombo Morada: Av. Lusíada, Centro Colombo Piso 1, Loja 1.095<br />
1500-392 Lisboa
</div>
<div class="Maia" style="height: 30%; width: 100%; background-color: #CCEAFF; margin-top: 2%;display:none;">
Timberland Picoas<br />
Centro Colombo Morada: Av. Lusíada, Centro Colombo Piso 1, Loja 1.095<br />
1500-392 Lisboa
</div>
</div>
脚本和类定义:
<script type="text/javascript">
function showstuff(element) {
var elementsPicoas = document.getElementsByClassName("Picoas");
elementsPicoas.style.display = element == "Picoas" ? "block" : "none";
var elementsBenfica = document.getElementsByClassName("Benfica");
elementsBenfica.style.display = element == "Benfica" ? "block" : "none";
var elementsFelgueiras = document.getElementsByClassName("Felgueiras");
elementsFelgueiras.style.display = element == "Felgueiras" ? "block" : "none";
var elementsMaia = document.getElementsByClassName("Maia");
elementsMaia.style.display = element == "Maia" ? "block" : "none";
}
</script>
<style>
.Picoas{}
.Felgueiras{}
.Benfica{}
.Maia{}
</style>