【发布时间】:2023-01-25 23:23:11
【问题描述】:
我不知道我在这段代码中做错了什么,我在网上看了看,我所看到的只是把 window.onload = function() 放在代码的开头。但是,该值始终打印为 null,我无法理解为什么要这样做。
这是 HTML:
<div class="filter-select-container">
<!-- filter selector -->
<div class="filter-selection-container">
<select name="select-filter" id="select-filter">
<option value="filter-all">All</option>
<option value="filter-commercials" data-sel="1">Commercials</option>
<option value="filter-fiction" data-sel="2">Fiction</option>
<option value="filter-music-videos" data-sel="3">Music Videos</option>
</select>
</div>
</div>
这是JS:
window.onload = function () {
// Get the select element by its id
const select = document.getElementById("select-filter");
// Get the selected option element
const selectedOption = select.options[select.selectedIndex];
// Get the data-select value
const dataSelect = selectedOption.getAttribute("data-sel");
// Print the data-select value to the console
console.log(dataSelect);
}
谢谢你的帮助 :)
【问题讨论】:
标签: javascript html css