修改CSS项值



对不起,我是新来的,有一个问题得到正确的代码。

我试图改变在leafet .js自定义标记的背景颜色。我基本上需要改变CSS元素的值。我有CSS和我如何使用它。我想在代码中改变。pin上的background-color,而不是CSS。

我已经尝试了这里的样本和jquery,但得到错误。

CSS是:

.location-pin {
display: inline-block;
position: relative;
top: 50%;
left: 50%;
}
.location-pin img {
width: 46px;
height: 46px;
margin: -26px 0 0 -13px;
z-index: 10;
position: absolute;
border-radius: 50%;
background: #32383e;
}
.pin {
width: 50px;
height: 50px;
border-radius: 50% 50% 50% 0;
background: #32383e;
position: absolute;
transform: rotate(-45deg);
left: 50%;
top: 50%;
margin: -43px 0 0 -30px;
}

和使用它的JS:

var client = L.divIcon({
className: 'location-pin',          
html: '<img id"operatorimg" src="img/test.jpg"><div class="pin"></div>',
iconSize: [30, 30],
iconAnchor: [18, 30]
});

var marker = L.marker(new L.LatLng(a[0], a[1]), {
icon: client,
title: title
});
marker.bindPopup(title);
markers.addLayer(marker);
}
map.addLayer(markers);

我试过了

$("pin").css("background:black");

但不工作?任何帮助将是感激的,谢谢你,抱歉,如果我没有正确地解释它或把它放在这里正确,我第一次使用它。由于

既然你创建的元素是"如果在运行时被添加到DOM中,并且不想更改css文件,你可以为你想使用的颜色创建额外的类

<div class="pin bgblack"></div>

或者你可以添加inline css

<div class="pin" style="background-color:#000"></div>

在您提供的代码中,您没有正确使用类选择器,'pin'是一个CSS类,因此应该使用'来选择。在它的名字前面。其次,css方法有不同的语法。

$(".pin").css("background-color", "black");

谢谢你的帮助,我最后是这么做的。

if (online_status === "YES") {
console.log('online');
var client = L.divIcon({
className: 'location-pin',          
html: '<img id"operatorimg" src="img/'+imgts+'.jpg"><div class="pin online"></div>',
iconSize: [30, 30],
iconAnchor: [18, 30]
});
var marker = L.marker(new L.LatLng(a[0], a[1]), {
icon: client,
title: title
});
}

else if (online_status === "NO") {
console.log('offline');
var client = L.divIcon({
className: 'location-pin',          
html: '<img id"operatorimg" src="img/'+imgts+'.jpg"><div class="pin offline"></div>',
iconSize: [30, 30],
iconAnchor: [18, 30]
});
var marker = L.marker(new L.LatLng(a[0], a[1]), {
icon: client,
title: title
});
}

我只是循环遍历数据,如果在线或离线,则将CSS更改为绿色或红色。

肯定有更快的方法用更少的代码,但现在就做,再次感谢

最新更新