我只能使用JavaScript來解決問題。我試圖寫一個for循環,但懸停時變得不確定。我基本上有一個動態生成的子菜單。當li在子菜單上懸停時,內部HTML將假設爲內容區域更改爲lis的右側。循環設置內部HTML
sideCol.getElementsByTagName('li')[0].addEventListener('mouseover', function(){
document.getElementsByClassName('categoryExplorer-content')[0].children[2].innerHTML = document.getElementsByClassName('categoryExplorer-content')[0].children[1].innerHTML;
});
sideCol.getElementsByTagName('li')[1].addEventListener('mouseover', function(){
document.getElementsByClassName('categoryExplorer-content')[0].children[2].innerHTML = document.getElementsByClassName('categoryExplorer-content')[1].children[1].innerHTML;
});
sideCol.getElementsByTagName('li')[2].addEventListener('mouseover', function(){
document.getElementsByClassName('categoryExplorer-content')[0].children[2].innerHTML = document.getElementsByClassName('categoryExplorer-content')[2].children[1].innerHTML;
});
sideCol.getElementsByTagName('li')[3].addEventListener('mouseover', function(){
document.getElementsByClassName('categoryExplorer-content')[0].children[2].innerHTML = document.getElementsByClassName('categoryExplorer-content')[3].children[1].innerHTML;
});
sideCol.getElementsByTagName('li')[4].addEventListener('mouseover', function(){
document.getElementsByClassName('categoryExplorer-content')[0].children[2].innerHTML = document.getElementsByClassName('categoryExplorer-content')[4].children[1].innerHTML;
});
問題是什麼? – undefined
'sideCol'中有什麼?你可以使用'document.querySelectorAll'來緩解你的痛苦。 –
問題在於代碼不是DRY,sideCol是創建的div元素。 –