簡體   English   中英

如何在單擊時更改 divIcon className?

[英]How can I change divIcon className on click?

我有一張傳單地圖,上面有幾個標記。 單擊標記時,應突出顯示該標記。 我使用divIcons並想更改整個圖標或它的 className,兩者都適合我。 目前我只是在 html 樹中更改創建的div的類以突出顯示它。 這很好用,但我是一個標記聚類器。 因此,div 被創建和轉儲一遍又一遍,我的高亮類在每次聚類時消失。

標記:

var markerIcon = L.divIcon({
   className: 'marker--default',
   html: "15",
   iconAnchor: [20, 20]
});
var markerIconActive = L.divIcon({
    className: 'marker--state--active',
    html: "15",
    iconAnchor: [20, 20]
});

標記集成:

var markers = L.markerClusterGroup({});

markers.addLayer(L.marker([50.919523, 6.940621], {icon: markerIcon})).on('click', onClick);

map.addLayer(markers);

點擊事件:

function onClick(e) {
   e.target.setIcon(markerIconActive);
}

注意:它不起作用。 我收到錯誤“e.target.setIcon 不是函數”。 看完紀錄片后,似乎沒有 divIcons 的 setIcon 方法。 如前所述,如果只更改 className,它也會對我有用,但我不知道如何更改這個。

文檔:

https://leafletjs.com/reference-1.4.0.html#divicon

與其嘗試通過e.target.<function>訪問函數/屬性, e.target.<function>使用e.<function>this.<function>

function onClick(e) {
   e.setIcon(markerIconActive);
}

或者,您可以通過e.layer訪問它:

function onClick(e) {
   let marker = e.layer;

   marker.setIcon(markerIconActive);
}

閱讀材料

有沒有辦法點擊任何地圖標記並檢索標記的緯度/經度(或數組索引)?

如果要更改 div 的類,本機函數是classList 您可以將它與stopPropagation一起使用,以防止您指出的第一個錯誤。 像這樣

function onClick(e) {
   e.stopPropagation();
   e.target.classList.toggle('newclass');
}

如果你想要一個“開關”,你也可以使用replace方法而不是切換

function onClick(e) {
   e.stopPropagation();
   e.target.classList.replace('oldclass','newclass');
}

調用時不要忘記通過 onClick 函數傳遞事件氣泡$.on('click', onClick(e))

markers.addLayer(L.marker([50.919523, 6.940621], {icon: markerIcon})).on('click', onClick(e));

我可以在里面有一個 leaflet divIcon 的 className <style scoped> section of a vue component?</div><div id = 'text_translate'><p>I have a Vue 2 sample project at <a href="https://github.com/ericg-vue-questions/leaflet-test" rel="nofollow noreferrer">https://github.com/ericg-vue-questions/leaflet-test</a></p> <p>When <a href="https://github.com/ericg-vue-questions/leaflet-test/blob/main/src/components/LeafletTest.vue" rel="nofollow noreferrer">declaring the divIcon</a>, I have:</p> <pre> const cloudIcon = L.divIcon({ html: thecloud, className: 'my-custom-icons', iconSize: [size, size], iconAnchor: [size/2, size/2] }) </pre> <p>and, for leaflet to see the my-custom-icons style, I wrote:</p> <pre>&lt;style scoped&gt; #mapContainer { width: 500px; height: 500px; } &lt;/style&gt; &lt;style&gt; .my-custom-icons { background-color: red; } &lt;/style&gt; </pre> <p>I would like the style section to look like:</p> <pre>&lt;style scoped&gt; #mapContainer { width: 500px; height: 500px; } .my-custom-icons { background-color: red; } &lt;/style&gt; </pre> <p>But when I place my-custom-icons inside of the components scoped section, the divIcon cannot find it.</p> <p>Can I organize my style section like this? If so, how?</p> </div></style>

[英]Can I have the className of a leaflet divIcon inside the <style scoped> section of a vue component?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 如何在單擊時更改元素的 className? (漢堡菜單) 我可以在里面有一個 leaflet divIcon 的 className <style scoped> section of a vue component?</div><div id = 'text_translate'><p>I have a Vue 2 sample project at <a href="https://github.com/ericg-vue-questions/leaflet-test" rel="nofollow noreferrer">https://github.com/ericg-vue-questions/leaflet-test</a></p> <p>When <a href="https://github.com/ericg-vue-questions/leaflet-test/blob/main/src/components/LeafletTest.vue" rel="nofollow noreferrer">declaring the divIcon</a>, I have:</p> <pre> const cloudIcon = L.divIcon({ html: thecloud, className: 'my-custom-icons', iconSize: [size, size], iconAnchor: [size/2, size/2] }) </pre> <p>and, for leaflet to see the my-custom-icons style, I wrote:</p> <pre>&lt;style scoped&gt; #mapContainer { width: 500px; height: 500px; } &lt;/style&gt; &lt;style&gt; .my-custom-icons { background-color: red; } &lt;/style&gt; </pre> <p>I would like the style section to look like:</p> <pre>&lt;style scoped&gt; #mapContainer { width: 500px; height: 500px; } .my-custom-icons { background-color: red; } &lt;/style&gt; </pre> <p>But when I place my-custom-icons inside of the components scoped section, the divIcon cannot find it.</p> <p>Can I organize my style section like this? If so, how?</p> </div></style> 單擊包含按鈕的 l.divicon 自定義 html 時如何阻止彈出窗口打開 如何直接加載 SVG 而不是通過 js 文件導入以在傳單 divIcon 中使用? 我可以選擇一個元素並像這樣更改 Classname 嗎? 更改頁面時可以在腳本中更改類名嗎 單擊React中的另一個元素后如何將className更改為元素 如何更改我的類名<img/>單擊按鈕時標記? 如何更改className? 如何根據 ReactJS 中的特定 className 觸發點擊事件
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM