在地图组件中使用原生js添加dom结构时 样式 伪类不生效

本文介绍了如何通过JavaScript动态创建DOM元素,并将其定位到地图上,以实现一个实时显示作业基础信息的工作计划提示框。作者展示了如何使用parseDom函数构建div元素,并在地图上添加Marker。同时讨论了样式穿透和删除DOM元素的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

手动创建dom对象 

       parseDom(arg) {
        var objE = document.createElement("div");
         objE.innerHTML = arg;
         return objE.childNodes;
      }, 

 创建div并在地图中定位

      enterMarkerPoint(e) {
        // let  el1 = document.createElement("div");
        let  el1 = this.parseDom(
        `<div id="workplanhint" class="workplanbox">
          <div>作业基础信息</div>
          <div>作业内容: </div>
          <div>风险等级: </div>
          <div>作业班组: </div>
          <div>作业人员: </div>
          <div>是否有违章: </div>   
            `
        )[0]
            el1.className = "workplanbox"
            console.log(el1,'112312312312312');                  
            new SGMap.Marker(el1).setLngLat(e.LngLat).addTo(this.map);
            console.log(this.map.boxZoom._el);  
            this.enterPoint("进入了Marker点"+e.id);
      },

 样式 伪类不生效的话使用 >>> 使用样式穿透

>>>.workplanbox{
   width: 308px;
   background-color: #011112;
   opacity: 0.7;
   font-size: 13px;
   color: #FFFFFF;
   position: relative;
   top: 48px;
   right: 48px;
   border-radius: 0 20px;
   border: 1px solid #2A62E3;
}
>>>.workplanbox::before{
    content: '';
    width: 15px;
    height: 24px;
    border-top: 3px solid #15A8FC;
    border-left: 3px solid #15A8FC;
    display: inline-block;
    position: absolute;
    top: -2px;
    left: -2px;
}
>>>.workplanbox::after{
    content: '';
    width: 15px;
    height: 24px;
    border-right: 3px solid #15A8FC;
    border-bottom: 3px solid #15A8FC;
    display: inline-block;
    position: relative;
    top: 6px;
    left: 294px;
}

删除添加的dom

地图对象.removeChild(要去除dom对象id)

(function(a){a.N={VERSION:"4.1.0",ROOT_URL:a.L_ROOT_URL||function(){var a=document.getElementsByTagName("script"),b=/\/?newmap[\-\._]?([\w\-\._]*)\.js\??/,c,d,e,f;for(c=0,d=a.length;c<d;c++){e=a[c].src,f=e.match(b);if(f)return f[1]==="include"?"../../dist/":e.replace(b,"")+"/"}return""}(),noConflict:function(){return a.N=this._original,this},_original:a.N}})(this),NUtil={extend:function(a){var b=Array.prototype.slice.call(arguments,1);for(var c=0,d=b.length,e;c2?Array.prototype.slice.call(arguments,2):null;return function(){return a.apply(b,c||arguments)}},tryFuncs:function(){var a=null;for(var b=0,c=arguments.length;b<c;b++){var d=arguments[b];try{a=d();break}catch(e){}}return a},getParameterString:function(a){var b=[];for(var c in a){var d=a[c];if(d!=null&&typeof d!="function"){var e;if(typeof d=="object"&&d.constructor==Array){var f=[],g;for(var h=0,i=d.length;h<i;h++)g=d[h],f.push(encodeURIComponent(g===null||g===undefined?"":g));e=f.join(",")}else e=encodeURIComponent(d);b.push(encodeURIComponent(c)+"="+e)}}return b.join("&")},containsStr:function(a,b){return a.indexOf(b)!=-1},getParameters:function(a){a=a===null||a===undefined?window.location.href:a;var b="";if(NUtil.containsStr(a,"?")){var c=a.indexOf("?")+1,d=NUtil.containsStr(a,"#")?a.indexOf("#"):a.length;b=a.substring(c,d)}var e={},f=b.split(/[&;]/);for(var g=0,h=f.length;g<h;++g){var i=f[g].split("=");if(i[0]){var j=i[0];try{j=decodeURIComponent(j)}catch(k){j=unescape(j)}var l=(i[1]||"").replace(/\+/g," ");try{l=decodeURIComponent(l)}catch(k){l=unescape(l)}l=l.split(","),l.length==1&&(l=l[0]),e[j]=l}}return e},urlAppend:function(a,b){var c=a;if(b){var d=(a+" ").split(/[?&]/);c+=d.pop()===" "?b:d.length?"&"+b:"?"+b}return c},upperCaseObject:function(a){var b={};for(var c in a)b[c.toUpperCase()]=a[c];return b},createUrlObject:function(a,b){b=b||{};if(!/^\w+:\/\//.test(a)){var c=window.location,d=c.port?":"+c.port:"",e=c.protocol+"//"+c.host.split(":").shift()+d;if(a.indexOf("/")===0)a=e+a;else{var f=c.pathname.split("/");f.pop(),a=e+f.join("/")+"/"+a}}b.ignoreCase&&(a=a.toLowerCase());var g=document.createElement("a");g.href=a;var h={};h.host=g.host.split(":").shift(),h.protocol=g.protocol,b.ignorePort80?h.port=g.port=="80"||g.port=="0"?"":g.port:h.port=g.port==""||g.port=="0"?"80":g.port,h.hash=b.ignoreHash||g.hash==="#"?"":g.hash;var i=g.search;if(!i){var j=a.indexOf("?");i=j!=-1?a.substr(j):""}return h.args=NUtil.getParameters(i),h.pathname=g.pathname.charAt(0)=="/"?g.pathname:"/"+g.pathname,h},stamp:function(){var a=0,b="_newmap_id";return function(c){return c[b]=c[b]||"_newmap_id_"+ ++a,c[b]}}(),requestAnimFrame:function(){function a(a){window.setTimeout(a,1e3/60)}var b=window.requestAnimationFrame||
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值