您好,
下面的脚本会在位置插件中永久显示工具提示,并为其着色,以便根据其类型(事件、地点等)进行排序。我正在开发一个图例,也许还有一个过滤器。
情况是这样的:它确实有效,但仅在我刷新页面后才有效,当我通过 URL 访问该页面时无效。
两种情况下控制台中的错误没有区别,增加函数的延迟时间也没有起作用。
有人在从“编辑主题”菜单将 JS 添加到标题时遇到过这种情况并找到了解决方案吗?
这是结果的图片
这里的错误在有或没有代码的情况下都会出现,所以它们与代码无关
<script>
document.addEventListener('DOMContentLoaded', function() {
// Apply CSS to change the alt text style
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = `
/* Ensure alt text is bold, underlined, and has conditional color */
.leaflet-marker-pane img[alt] {
font-weight: bold !important; /* Make text bold */
text-decoration: underline !important; /* Underline the text */
white-space: nowrap; /* Prevent the text from wrapping to a new line */
}
`;
document.head.appendChild(style);
// Give the page a moment to load and the images to be rendered
setTimeout(function() {
var images = document.querySelectorAll('.leaflet-marker-pane img');
images.forEach(function(img) {
var title = img.getAttribute('title');
if (title) {
title = title.length > 10 ? title.substring(0, 10) + '..' : title;
img.setAttribute('alt', title);
img.setAttribute('title', title);
if (title.includes('/')) {
img.style.color = 'white';
} else {
img.style.color = 'black';
}
}
img.setAttribute('src', '');
});
}, 1000); // Delay execution by 1 second (1000 ms)
});
</script>

