【发布时间】:2020-06-17 01:31:08
【问题描述】:
我正在尝试通过更改其描边颜色来突出显示 SVG 中的特定多边形。
不幸的是,第二个多边形的笔划完全覆盖了一侧。
我曾尝试使用 z 索引将其置于最前面,但没有成功。
<button id="b1" type="button">1st</button>
<svg viewBox="0 0 1240 1000">
<g>
<polygon class="map__1" id="pol1" points="106.75,266 4,266 4,135 106.75,135 106.75,266" data-id="1">
</polygon>
<polygon class="map__2" points="178.25,168.655 106.75,240 106.75,135 145.75,135 178.25,168.655" data-id="2"></polygon>
</g>
</svg>
js
let btn1 = document.getElementById("b1");
let pol1 = document.getElementById("pol1");
btn1.addEventListener("click", myFunction);
function myFunction() {
pol1.style.stroke = "#fc0303";
}
css
.map__1, .map__2 {
stroke: #000;
stroke-width: 5px;
stroke-miterlimit: 10;
fill: #6e6e6e;
}
这是小提琴:https://jsfiddle.net/tfzbjxL3/
我也尝试过使用 outline 属性,但它不适合其他类型的多边形,而是正方形。
我有什么办法可以做到这一点?
谢谢!
【问题讨论】:
-
z-index 在 SVG 中不起作用。相反,您可以使用
appendChild在 javascript 中附加元素请看一下这支笔:codepen.io/osublake/pen/YXoEQe?editors=1010
标签: javascript html css svg frontend