pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <legend id='RFUqi'><style id='RFUqi'><dir id='RFUqi'><q id='RFUqi'></q></dir></style></legend>
  • <tfoot id='RFUqi'></tfoot>
    1. <small id='RFUqi'></small><noframes id='RFUqi'>

        <bdo id='RFUqi'></bdo><ul id='RFUqi'></ul>

        <i id='RFUqi'><tr id='RFUqi'><dt id='RFUqi'><q id='RFUqi'><span id='RFUqi'><b id='RFUqi'><form id='RFUqi'><ins id='RFUqi'></ins><ul id='RFUqi'></ul><sub id='RFUqi'></sub></form><legend id='RFUqi'></legend><bdo id='RFUqi'><pre id='RFUqi'><center id='RFUqi'></center></pre></bdo></b><th id='RFUqi'></th></span></q></dt></tr></i><div class="r8icxvm" id='RFUqi'><tfoot id='RFUqi'></tfoot><dl id='RFUqi'><fieldset id='RFUqi'></fieldset></dl></div>
      1. 如何檢查 svg 路徑是否具有與數組中的值匹配的類

        How to check if a svg path has a class that matches a value in array and if so add a new class(如何檢查 svg 路徑是否具有與數組中的值匹配的類,如果是,則添加新類)

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

      2. <tfoot id='CGkfE'></tfoot>
          <bdo id='CGkfE'></bdo><ul id='CGkfE'></ul>

        • <i id='CGkfE'><tr id='CGkfE'><dt id='CGkfE'><q id='CGkfE'><span id='CGkfE'><b id='CGkfE'><form id='CGkfE'><ins id='CGkfE'></ins><ul id='CGkfE'></ul><sub id='CGkfE'></sub></form><legend id='CGkfE'></legend><bdo id='CGkfE'><pre id='CGkfE'><center id='CGkfE'></center></pre></bdo></b><th id='CGkfE'></th></span></q></dt></tr></i><div class="vr7jlt5" id='CGkfE'><tfoot id='CGkfE'></tfoot><dl id='CGkfE'><fieldset id='CGkfE'></fieldset></dl></div>
            <tbody id='CGkfE'></tbody>

                  <legend id='CGkfE'><style id='CGkfE'><dir id='CGkfE'><q id='CGkfE'></q></dir></style></legend>
                  本文介紹了如何檢查 svg 路徑是否具有與數組中的值匹配的類,如果是,則添加新類的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有一個數組和一些 svg path 元素(我正在使用 傳單地圖).我需要檢查路徑的類是否與我的數組中的某個值匹配,如果是,請向其添加一個類 fadeIn.

                  I have an array and some svg path elements (I am using leaflet map). I need to check if the class of a path matches one of the values in my array and if so add a class fadeIn to it.

                  var foundNations = ["usa", "France", "Italy"];
                  document.querySelectorAll('path').forEach(path => {
                    if (foundNations.includes(path.className)) {
                     path.className.add('fadeIn');
                      console.log(path.className);
                    }
                  });
                  (function($) {
                      var map = L.map('map').setView([45.4655171, 12.7700794], 2);
                      map.fitWorld().zoomIn();
                      L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
                        attribution: 'Map data &copy; <a >OpenStreetMap</a> contributors, ' + '<a  + 'Imagery ? <a ,
                        id: 'mapbox.light'
                      }).addTo(map);
                  
                      var mapHalfHeight = map.getSize().y / 2,
                      container = map.zoomControl.getContainer(),
                      containerHalfHeight = parseInt(container.offsetHeight / 2),
                      containerTop = mapHalfHeight - containerHalfHeight + 'px';
                  
                      container.style.position = 'absolute';
                      container.style.top = containerTop;
                      map.scrollWheelZoom.disable();
                      var southWest = L.latLng(-89.98155760646617, -180),
                      northEast = L.latLng(89.99346179538875, 180);
                      var bounds = L.latLngBounds(southWest, northEast);
                      map.setMaxBounds(bounds);
                      map.on('drag', function() {
                        map.panInsideBounds(bounds, { animate: false });
                      });
                  
                    // get color depending on population density value
                      function getColor(d) {
                        return d > 1000 ? '#800026' :
                          d > 500  ? '#BD0026' :
                          d > 200  ? '#E31A1C' :
                          d > 100  ? '#FC4E2A' :
                          d > 50   ? '#FD8D3C' :
                          d > 20   ? '#FEB24C' :
                          d > 10   ? '#FED976' :
                                '#FFEDA0';
                      }
                  
                      function style(feature) {
                        return {
                          weight: 1,
                          opacity: 1,
                          color: '#ffffff',
                          dashArray: '',
                          fillOpacity: 0,
                          fillColor : '#FF0080',
                          className: feature.properties.name
                        };
                      }
                  
                      var geojson;
                  
                      function selectNation(e) {
                  
                      }
                  
                  
                     function onEachFeature(feature, layer) {
                        layer.on({
                          click: selectNation
                        });
                      }
                  
                      geojson = L.geoJson(statesData, {
                        style: style,
                        onEachFeature: onEachFeature
                      }).addTo(map);
                  })( jQuery );

                  #map {
                    width: 100vw;
                    height: 100vh;
                  }
                  
                  .fadeIn {
                    fill: red;
                  }

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                  <script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"></script>
                  <link  rel="stylesheet"/>
                  <script src="https://www.jikupin.com/world.json"></script>
                  
                  <div id='map'></div>

                  推薦答案

                  你的錯誤很微妙.您從 Strings:

                  Your bug is subtle. You are starting with an Array of Strings:

                  var nationList = ["usa", "france", "italy"];
                  

                  然后你調用 String.prototype.includes,將 path.className 作為參數傳遞.

                  Then you're calling String.prototype.includes, passing path.className as an argument.

                  if (foundNations.includes(path.className)) { path.classList.add('fadeIn')
                  

                  在那里,您隱含地假設 path.className 是一個String.但是,令人驚訝的是,它不是 String,而是 SVGAnimatedString!

                  In there, you're implicitly assuming that path.className is a String. But, surprise surprise, it's not a String, it's a SVGAnimatedString!

                  console.log(path.className)
                  > [object SVGAnimatedString] {
                     animVal: "germany",
                     baseVal: "germany"
                    }
                  

                  是的,在某些極端情況下,SVG 元素的類名可以在動畫期間進行修改.

                  Yes, class names for SVG elements can be modified during animations in some edge cases.

                  您可能想要做的是使用 SVGAnimatedStrings 的>baseVal 屬性:

                  What you probably want to do is use the baseVal property of the SVGAnimatedStrings:

                  console.log(typeof path.className.baseVal)
                  > "string"
                  

                  現在一切都應該按照您期望的方式更緊密地工作:

                  And now everything should work more closely to the way you expect:

                  if (foundNations.includes(path.className.baseVal)) {
                    path.classList.add('fadeIn')
                  }
                  console.log(path.className.baseVal);
                  > "spain fadeIn"
                  

                  <小時><小時>

                  由于另一個假設,您還有第二個問題.您假設 path.className 只包含 一個 類名,但 根據文檔,強調我的:



                  You have a second problem, due to another assumption. You're assuming that path.className contains just one class name, but according to the documentation, emphasis mine:

                  cName 是一個字符串變量,表示當前元素的類或空格分隔的類.

                  cName is a string variable representing the class or space-separated classes of the current element.

                  事實上,如果您使用瀏覽器中提供的開發人員工具來檢查 SVG 元素,您會看到類似...

                  In fact, if you use the developer tools available in your browser to inspect the SVG elements, you'll see things like...

                  <path class="Italy leaflet-interactive" stroke="#ffffff" ....></path>

                  所以在這種情況下,您假設 className.baseVal 將是字符串 "Italy",但實際上,它的值是 意大利傳單互動".

                  So in this case, you're assuming that the className.baseVal is going to be the string "Italy", but in reality, it takes the value "Italy leaflet-interactive".

                  這里的方法是使用 Element.classList 遍歷類名s 以查看其中是否有 任何 匹配給定組.

                  The approach here is to use Element.classList to iterate through the class names to see if any of them match a given group.

                  此外,我認為這是 XY 問題的一個實例.我想你不想問

                  Furthermore, I think that this is an instance of the XY problem. I don't think you wanted to ask

                  如何檢查 SVG 路徑是否有一個匹配 foo 的類?

                  How to check if a SVG path has a class that maches foo?

                  而是

                  當特征匹配 foo 時,如何在 Leaflet 中符號化 SVG 多邊形?

                  How to symbolize a SVG polygon in Leaflet when the feature matches foo?

                  因為我認為將檢查移到 style 回調函數中更優雅,例如:

                  Because I think that it is way more elegant to move the checks into the style callback function, like:

                  geojson = L.geoJson(statesData, {
                    style: function(feature){ 
                      var polygonClassName = feature.properties.name;
                      if (nationList.contains(feature.properties.name)) {
                          polygonClassName += ' fadeIn';
                      }
                      return {
                        weight: 1,
                        opacity: 1,
                        color: '#ffffff',
                        dashArray: '',
                        fillOpacity: 0,
                        fillColor : '#FF0080',
                        className: polygonClassName
                      };
                    },
                    onEachFeature: onEachFeature
                  }).addTo(map);
                  

                  Leaflet 提供了方便的功能,例如 L.Path.setStyle 隱藏了直接處理選擇器和 SVG 類的復雜性,只要您保留對 L.Polygon 實例的引用(在這種情況下,您可以這樣做在 onEachFeature 回調中).

                  Leaflet offers convenient functionality like L.Path.setStyle that hides the complexity of dealing with selectors and SVG classes directly, as long as you keep references to your instances of L.Polygon around (which, in this case, you can do in the onEachFeature callback).

                  這篇關于如何檢查 svg 路徑是否具有與數組中的值匹配的類,如果是,則添加新類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Check if a polygon point is inside another in leaflet(檢查一個多邊形點是否在傳單中的另一個內部)
                  Changing leaflet markercluster icon color, inheriting the rest of the default CSS properties(更改傳單標記群集圖標顏色,繼承其余默認 CSS 屬性)
                  Trigger click on leaflet marker(觸發點擊傳單標記)
                  How can I change the default loading tile color in LeafletJS?(如何更改 LeafletJS 中的默認加載磁貼顏色?)
                  Adding Leaflet layer control to sidebar(將 Leaflet 圖層控件添加到側邊欄)
                  Leaflet - get latitude and longitude of a marker inside a pop-up(Leaflet - 在彈出窗口中獲取標記的緯度和經度)

                  1. <i id='7uEeb'><tr id='7uEeb'><dt id='7uEeb'><q id='7uEeb'><span id='7uEeb'><b id='7uEeb'><form id='7uEeb'><ins id='7uEeb'></ins><ul id='7uEeb'></ul><sub id='7uEeb'></sub></form><legend id='7uEeb'></legend><bdo id='7uEeb'><pre id='7uEeb'><center id='7uEeb'></center></pre></bdo></b><th id='7uEeb'></th></span></q></dt></tr></i><div class="b7h57vf" id='7uEeb'><tfoot id='7uEeb'></tfoot><dl id='7uEeb'><fieldset id='7uEeb'></fieldset></dl></div>
                      <tbody id='7uEeb'></tbody>
                      <bdo id='7uEeb'></bdo><ul id='7uEeb'></ul>

                      <small id='7uEeb'></small><noframes id='7uEeb'>

                      1. <tfoot id='7uEeb'></tfoot>
                        <legend id='7uEeb'><style id='7uEeb'><dir id='7uEeb'><q id='7uEeb'></q></dir></style></legend>
                          • 主站蜘蛛池模板: 碳刷_刷握_集电环_恒压簧_电刷厂家-上海丹臻机电科技有限公司 | 并离网逆变器_高频UPS电源定制_户用储能光伏逆变器厂家-深圳市索克新能源 | 齿轮减速电机一体机_蜗轮蜗杆减速马达-德国BOSERL齿轮减速机带电机生产厂家 | PTFE接头|聚四氟乙烯螺丝|阀门|薄膜|消解罐|聚四氟乙烯球-嘉兴市方圆氟塑制品有限公司 | 南京蜂窝纸箱_南京木托盘_南京纸托盘-南京博恒包装有限公司 | 机床主轴维修|刀塔维修|C轴维修-常州翔高精密机械有限公司 | 多功能真空滤油机_润滑油全自动滤油机_高效真空滤油机价格-重庆润华通驰 | 盘古网络技术有限公司| 高清视频编码器,4K音视频编解码器,直播编码器,流媒体服务器,深圳海威视讯技术有限公司 | 胜为光纤光缆_光纤跳线_单模尾纤_光纤收发器_ODF光纤配线架厂家直销_北京睿创胜为科技有限公司 - 北京睿创胜为科技有限公司 | 掺铥光纤放大器-C/L波段光纤放大器-小信号光纤放大器-合肥脉锐光电技术有限公司 | HEYL硬度计量泵-荧光法在线溶解氧仪-净时测控技术(上海)有限公司 | 成都热收缩包装机_袖口式膜包机_高速塑封机价格_全自动封切机器_大型套膜机厂家 | 硫酸亚铁-聚合硫酸铁-除氟除磷剂-复合碳源-污水处理药剂厂家—长隆科技 | 沈阳缠绕包装机厂家直销-沈阳海鹞托盘缠绕包装机价格 | 冷藏车厂家|冷藏车价格|小型冷藏车|散装饲料车厂家|程力专用汽车股份有限公司销售十二分公司 | 山东led显示屏,山东led全彩显示屏,山东LED小间距屏,临沂全彩电子屏-山东亚泰视讯传媒有限公司 | 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 座椅式升降机_无障碍升降平台_残疾人升降平台-南京明顺机械设备有限公司 | 蒸压釜_蒸养釜_蒸压釜厂家-山东鑫泰鑫智能装备有限公司 | 武汉刮刮奖_刮刮卡印刷厂_为企业提供门票印刷_武汉合格证印刷_现金劵代金券印刷制作 - 武汉泽雅印刷有限公司 | 螺旋叶片_螺旋叶片成型机_绞龙叶片_莱州源泽机械制造有限公司 | 防爆正压柜厂家_防爆配电箱_防爆控制箱_防爆空调_-盛通防爆 | 工业铝型材-铝合金电机壳-铝排-气动执行器-山东永恒能源集团有限公司 | 泰州物流公司_泰州货运公司_泰州物流专线-东鑫物流公司 | 新型游乐设备,360大摆锤游乐设备「诚信厂家」-山东方鑫游乐设备 新能源汽车电池软连接,铜铝复合膜柔性连接,电力母排-容发智能科技(无锡)有限公司 | 高温热泵烘干机,高温烘干热泵,热水设备机组_正旭热泵 | 贝朗斯动力商城(BRCPOWER.COM) - 买叉车蓄电池上贝朗斯商城,价格更超值,品质有保障! | 翅片管换热器「型号全」_厂家-淄博鑫科环保 | 农产品溯源系统_农产品质量安全追溯系统_溯源系统 | 淄博不锈钢无缝管,淄博不锈钢管-鑫门物资有限公司 | 船用泵,船用离心泵,船用喷射泵,泰州隆华船舶设备有限公司 | bng防爆挠性连接管-定做金属防爆挠性管-依客思防爆科技 | 单锥双螺旋混合机_双螺旋锥形混合机-无锡新洋设备科技有限公司 | 球形钽粉_球形钨粉_纳米粉末_难熔金属粉末-广东银纳官网 | 车充外壳,车载充电器外壳,车载点烟器外壳,点烟器连接头,旅行充充电器外壳,手机充电器外壳,深圳市华科达塑胶五金有限公司 | RTO换向阀_VOC高温阀门_加热炉切断阀_双偏心软密封蝶阀_煤气蝶阀_提升阀-湖北霍科德阀门有限公司 | 牛皮纸|牛卡纸|进口牛皮纸|食品级牛皮纸|牛皮纸厂家-伽立实业 | 无机纤维喷涂棉-喷涂棉施工工程-山东华泉建筑工程有限公司▲ | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 丙烷/液氧/液氮气化器,丙烷/液氧/液氮汽化器-无锡舍勒能源科技有限公司 |