我有一个带有init函数的对象,正在初始化选择输入字段。
SelectBuilder.init();
init方法如下:
init: () => {
const select = new MDCSelect(document.querySelector('.mdc-select'));
select.listen('MDCSelect:change', async () => {
if (select.value === state.data.selectedLocation) {
return;
}
state.setState({ selectedLocation: select.value });
let regions = geodata;
let center = MAP_CENTER;
let zoomLevel = window.screen.width < 600 ? 6 : 7;
googleMap.panTo(center);
map.setZoom(zoomLevel);
drawRegions(googleMap, regions);
});
window.onload = async function () {
console.log('loaded');
const locations = await import('./assets/data/locations.json');
const sortedLocations = locations.sort((a, b) => a.Name.localeCompare(b.Name));
// init select items
sortedLocations.forEach(location => {
const list = document.querySelector('.mdc-list');
const newListItem = document.createElement('li');
newListItem.setAttribute('class', 'mdc-list-item');
newListItem.setAttribute('data-value', location.ID);
const listeItemIcon = document.createElement('i');
listeItemIcon.setAttribute('class', 'fas fa-map-marker-alt');
const listeItemText = document.createElement('span');
listeItemText.setAttribute('class', 'mdc-list-item__text');
listeItemText.appendChild(document.createTextNode(location.Name));
newListItem.appendChild(listeItemIcon);
newListItem.appendChild(listeItemText);
list.appendChild(newListItem);
});
};
}
从那时起,我想将选择菜单项的构建推迟到页面完全加载后,再将其包装在window onload事件中。但是,我可以在控制台中看到该事件从未触发过。我在这里做什么错了?