gpt4 book ai didi

javascript - 防止 SVG CSS 流血

转载 作者:行者123 更新时间:2023-11-28 19:23:29 25 4
gpt4 key购买 nike

基本上我有一个包含两个 SVG 的网页。单击后,所选 SVG 将可见。

问题:如果先加载视口(viewport)为 0 0 20 20 的 SVG,笔划宽度为 2,然后再加载视口(viewport)为 0 0 2000 2000 ,第一个的stroke-width继承给第二个。第二个现在的笔画宽度为 2 而不是 200。

容器是这样的:

<div class="clearView-container">
// svg 2
</div>
<div class="techView-container" style="display: none;">
// svg 1
</div>

svg1:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="273.263mm" height="210.784mm" viewBox="-49.35 -56.0421 136.632 105.392">

<defs>
<style type="text/css">
.pen_style1 {stroke: #000000;stroke-width: 0.25;}
.pen_style3 {stroke: #c6c6c6;stroke-width: 0.125;stroke-dasharray: 1, 0.5}
.pen_style4 {stroke: #ff0000;stroke-width: 0.125;stroke-dasharray: 0.2, 0.5, 1, 0.5}
.cos {stroke: #0037a0;}
.hiddenLine { display: none; }
</style>
</defs>

svg2:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="127.46mm" height="104.6mm" viewBox="-3214 -2698 6373 5230">

<defs>
<style type="text/css">
.pen_style1 {stroke: #000000;stroke-width: 25;}
.pen_style3 {stroke: #c6c6c6;stroke-width: 12.5;stroke-dasharray: 100, 50}
.pen_style4 {stroke: #ff0000;stroke-width: 12.5;stroke-dasharray: 20, 50, 100, 50}
.cos {stroke: #0037a0;}
.hiddenLine { display: none; }
</style>
</defs>

现在 clearView-container 中的 SVG从 techView-container 中获取一个的属性如果首先加载这个 (svg1)。

有没有办法阻止 <defs>两个 SVG 中的哪一个要“渗透”?

最佳答案

以防有人需要答案。这就是我最终这样做的方式。我从服务器获取 svg 作为字符串。比我使用带有 ID 的@DBSs 解决方案(不能在服务器端执行,所以这里是:)

function _injectCustomCSS (svg: string): string {
const newID = 'id' + MathLib.hashCode(svg);
const replaceStr = /.pen/g;
const svgDoc = new DOMParser().parseFromString(svg, 'image/svg+xml');
svgDoc.getElementsByTagName('svg')[0].setAttribute('id', newID);
svgDoc.getElementsByTagName('style')[0].textContent = svgDoc.getElementsByTagName('style')[0].textContent.replace(replaceStr, '#' + newID + '>.pen');

/* ... */
return new XMLSerializer().serializeToString(svgDoc);
};

关于javascript - 防止 SVG CSS 流血,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56848032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com