【发布时间】:2021-12-17 22:29:04
【问题描述】:
有没有办法从 HTML 选择元素中获取选项的 style.color 值
以这种形式,或者是否有任何方法可以从该选择元素内部获取特定子项的属性
注意:
当我将选项元素 id 更改为 class 时,网站停止工作
let continentsSelect = document.getElementById("continent");
let countriesSelect = document.getElementById("country");
let continentColor = document.getElementById("continent-color");
continentsSelect.style.color = continentColor.style.color
<select name="continent" id="continent">
<option id="continent-color" value="none" style="color: yellow;">اختر قارة</option>
<option id="continent-color" value="africa" style="color: green;">افريقيا</option>
<option id="continent-color" value="asia" style="color: green;">اسيا</option>
<option id="continent-color" value="europe" style="color: green;">اوربا</option>
<option id="continent-color" value="northAmirca" style="color: green;">امريكا الشمالية</option>
<option id="continent-color" value="southAmirca" style="color: green;">امريكا الجنوبية</option>
<option id="continent-color" value="oceania" style="color: green;">الاوقيانوسية</option>
</select>
【问题讨论】:
-
id必须是唯一的,使用data-attribute或class代替。
标签: html css select html-select styling