在導(dǎo)航欄中單擊“圖例”時(shí),我正在嘗試進(jìn)行開/關(guān)切換。但是,當(dāng)我為 構(gòu)造函數(shù)時(shí)addEventListener,我不斷收到類型錯(cuò)誤。我知道這是因?yàn)閕nfo未定義,但我不確定如何定義它,因此style display可以更改為,當(dāng)在導(dǎo)航欄中單擊block時(shí)它會(huì)出現(xiàn)在地圖上。Legend有什么建議可以為我指明正確的方向嗎?HTML:<!--Navbar--> <nav class="navbar"> <div class="logo"> <h4> Earthquake Feed</h4> </div> <ul class="nav-links"> <li><a href="#simpleModal" id="aboutModal">About</a></li> <li><a href="#legend" id="legend">Legend</a></li> </ul> </nav>CSS:/*CSS for legend*/.info { padding: 6px 8px; font: 14px/16px Arial, Helvetica, sans-serif; background: white; background: rgba(255,255,255,0.8); box-shadow: 0 0 15px rgba(0,0,0,0.2); border-radius: 5px; display: none; }.legend { background-color: “black”; line-height: 26px; color: #555; width: auto;Javascript:// adds legend to mapvar legend = L.control({position: 'bottomright'});legend.onAdd = function(map) { var div = L.DomUtil.create('div', 'info legend'), magnitudes = [0,1,2,3,4,5]; div.innerHTML += "<h4 style='margin:5px'>Magnitude</h4>" div.innerHTML += '<i class="circleR" style=background:>' + '</i>' + "Recent" + '<br>' // loop through color intervals and generate a lable for (var i=0; i < magnitudes.length; i++) { div.innerHTML += '<i class="circle" style=background:' + getColor(magnitudes[i] + 1) + '></i>' + magnitudes[i] + (magnitudes[i+1]?'–' + magnitudes[i+1] + '<br>': '+'); } return div;};legend.addTo(map);// toggles legend on and offvar legendLink = document.getElementById("legend");legendLink.addEventListener('click', openLegend);function openLegend(){ info.style.display = 'block';}
通過 eventListener 打開/關(guān)閉傳單圖例
暮色呼如
2022-01-13 19:22:52