<bdo id='cNY8d'></bdo><ul id='cNY8d'></ul>
<legend id='cNY8d'><style id='cNY8d'><dir id='cNY8d'><q id='cNY8d'></q></dir></style></legend>

  • <tfoot id='cNY8d'></tfoot>

    1. <small id='cNY8d'></small><noframes id='cNY8d'>

      <i id='cNY8d'><tr id='cNY8d'><dt id='cNY8d'><q id='cNY8d'><span id='cNY8d'><b id='cNY8d'><form id='cNY8d'><ins id='cNY8d'></ins><ul id='cNY8d'></ul><sub id='cNY8d'></sub></form><legend id='cNY8d'></legend><bdo id='cNY8d'><pre id='cNY8d'><center id='cNY8d'></center></pre></bdo></b><th id='cNY8d'></th></span></q></dt></tr></i><div id='cNY8d'><tfoot id='cNY8d'></tfoot><dl id='cNY8d'><fieldset id='cNY8d'></fieldset></dl></div>

        触发点击传单标记

        时间:2023-08-09
        <tfoot id='tL3ji'></tfoot>

          <small id='tL3ji'></small><noframes id='tL3ji'>

              <tbody id='tL3ji'></tbody>
            1. <i id='tL3ji'><tr id='tL3ji'><dt id='tL3ji'><q id='tL3ji'><span id='tL3ji'><b id='tL3ji'><form id='tL3ji'><ins id='tL3ji'></ins><ul id='tL3ji'></ul><sub id='tL3ji'></sub></form><legend id='tL3ji'></legend><bdo id='tL3ji'><pre id='tL3ji'><center id='tL3ji'></center></pre></bdo></b><th id='tL3ji'></th></span></q></dt></tr></i><div id='tL3ji'><tfoot id='tL3ji'></tfoot><dl id='tL3ji'><fieldset id='tL3ji'></fieldset></dl></div>
              <legend id='tL3ji'><style id='tL3ji'><dir id='tL3ji'><q id='tL3ji'></q></dir></style></legend>
                <bdo id='tL3ji'></bdo><ul id='tL3ji'></ul>

                • 本文介绍了触发点击传单标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在地图上有一堆传单标记.每个标记都保存在数组 markers 中.标记是动态创建的(在 ajax 调用期间).

                  I have a bunch of leaflet markers on the map. Each marker is held in array markers. The markers are created dynamically (during an ajax call).

                  var markers = [];
                  .
                  .
                  var marker = L.marker([mar.lat, mar.lng], {
                    // ...build the marker...
                  }
                  marker._leaflet_id = mar.id; // give the marker an id corresponding to the id of its corresponding div
                  var myHoverIcon = L.icon({
                    iconUrl: mar.imgUrl,
                    iconSize: [40, 40],
                    popupAnchor: [0, 0]
                  });
                  marker.on('click', function(e) {
                    alert('Marker clicked!');
                    marker.setIcon(myHoverIcon);
                  });
                  .
                  .
                  markers.push(marker);
                  

                  每个标记都有一个对应于特定 div 的 id(存储在 div 上的 data-mess_id 中).计划是在点击提要中相应的 div 时更改标记的图标.

                  Each marker has an id corresponding to a particular div (stored in data-mess_id on the div). The plan is to change the marker's icon when its corresponding div in the feed is clicked on.

                  $('#feed').on('mouseover', '.message', function() {
                    var cssid = $(this).attr('data-mess_id').toString();
                    var baz = $.grep(markers, function(m) {
                      return (m._leaflet_id == cssid);
                    });
                    baz[0].trigger('click');   // doesn't work 
                    alert(baz[0].getLatLng()); // does work
                  
                    // this also does not work:
                    var myHoverIcon = L.icon({
                      iconUrl: baz[0].imgUrl,
                      iconSize: [40, 40],
                      popupAnchor: [0, 0]
                    });
                    baz[0].setIcon(myHoverIcon);
                  
                  });
                  

                  除了最后一点,一切都很好.我只是无法在标记上触发点击事件.我肯定有正确的标记,因为 baz[0].getLatLng() 正在工作.但是 baz[0].trigger('click') 不起作用.

                  It's all working fine except for the final bit. I just can't trigger a click event on the marker. I definitely have the correct marker because baz[0].getLatLng() is working. But baz[0].trigger('click') doesn't work.

                  我尝试动态创建一个新图标 (myHoverIcon) 但它不起作用.

                  I tried creating a new icon dynamically (myHoverIcon) but it doesn't work.

                  如何在标记上触发点击事件?

                  How do I trigger a click event on the marker?

                  还有其他方法可以更改标记图标吗?

                  Is there another way to change the marker icon?

                  推荐答案

                  要模拟鼠标点击,您可以使用 fire 方法(继承自 Evented.fire) 在标记上:

                  To emulate a mouse click, you can use the fire method (inherited from Evented.fire) on the marker :

                  marker.fire('click');
                  

                  还有一个演示

                  var map = L.map('map').setView([0, 0], 12);
                  
                  var icon = L.icon({
                    iconUrl: 'http://leafletjs.com/examples/custom-icons/leaf-green.png'
                  });
                  var marker = L.marker([0, 0], {icon: icon})
                    .addTo(map);
                    
                    
                  var myHoverIcon = L.icon({
                    iconUrl: 'http://leafletjs.com/examples/custom-icons/leaf-red.png'
                  });
                    
                    
                  marker.on('click', function(e) {
                    marker.setIcon(myHoverIcon);
                  });
                  
                  document.querySelector('button').addEventListener('click', function() {
                    marker.fire('click');
                  });

                  html, body {
                    height: 100%;
                    margin: 0;
                  }
                  #map {
                    width: 100%;
                    height: 100%;
                  }
                  
                  button {position: absolute; left:10 px; top: 70px;}

                  <link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" integrity="sha512-M2wvCLH6DSRazYeZRIm1JnYyh22purTM+FDB5CsyxtQJYeKq83arPe5wgbNmcFXGqiSH2XR8dT/fJISVA1r/zQ==" crossorigin=""/>
                  
                  <script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js" integrity="sha512-lInM/apFSqyy1o6s89K4iQUKg6ppXEgsVxT35HbzUupEVRh2Eu9Wdl4tHj7dZO0s1uvplcYGmt3498TtHq+log==" crossorigin=""></script>
                     
                  <div id='map'></div>
                  <button>Click me</button>

                  这篇关于触发点击传单标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何更改 LeafletJS 中的默认加载磁贴颜色? 下一篇:更改传单标记群集图标颜色,继承其余默认 CSS

                  相关文章

                  最新文章

                    <small id='9iszP'></small><noframes id='9iszP'>

                      <tfoot id='9iszP'></tfoot>
                      <i id='9iszP'><tr id='9iszP'><dt id='9iszP'><q id='9iszP'><span id='9iszP'><b id='9iszP'><form id='9iszP'><ins id='9iszP'></ins><ul id='9iszP'></ul><sub id='9iszP'></sub></form><legend id='9iszP'></legend><bdo id='9iszP'><pre id='9iszP'><center id='9iszP'></center></pre></bdo></b><th id='9iszP'></th></span></q></dt></tr></i><div id='9iszP'><tfoot id='9iszP'></tfoot><dl id='9iszP'><fieldset id='9iszP'></fieldset></dl></div>
                    1. <legend id='9iszP'><style id='9iszP'><dir id='9iszP'><q id='9iszP'></q></dir></style></legend>
                      • <bdo id='9iszP'></bdo><ul id='9iszP'></ul>