我从我的设计师那里得到了svgs图标,如下所示:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16px" height="16px" viewBox="0 0 16 16" version="1.1">
<!-- Generator: Sketch 49 (51002) - http://www.bohemiancoding.com/sketch -->
<title>icons/Search</title>
<desc>Created with Sketch.</desc>
<defs>
<path d="M19.9540595,19.3616366 L15.8890991,15.2679046 C18.3035819,12.5133747 18.0987254,8.33903567 15.4261496,5.83443006 C12.7535738,3.32982445 8.57707245,3.39817204 5.98778844,5.98888691 C3.39850443,8.57960179 3.33019459,12.7584111 5.8334169,15.4324638 C8.33663921,18.1065165 12.5086727,18.3114862 15.2616813,15.8956692 L19.3619892,19.9540341 C19.5318715,20.0382483 19.7365147,20.0046375 19.8705748,19.8705033 C20.0046349,19.7363691 20.0382272,19.5316129 19.9540595,19.3616366 Z M10.7813881,16.7091105 C7.53099588,16.7091105 4.8960324,14.0726909 4.8960324,10.8205025 C4.8960324,7.56831416 7.53099588,4.93189457 10.7813881,4.93189457 C14.0317802,4.93189457 16.6667437,7.56831416 16.6667437,10.8205025 C16.6571029,14.0520297 14.0464342,16.6720327 10.8167355,16.691427 L10.7813881,16.7091105 Z" id="path-1"/>
</defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Artboard" transform="translate(-704.000000, -382.000000)">
<g id="icons/Search" transform="translate(700.000000, 378.000000)">
<mask id="mask-2" fill="white">
<use xlink:href="#path-1"/>
</mask>
<use id="Search" fill="#9CA0A0" fill-rule="evenodd" xlink:href="#path-1"/>
<g id="mixin/primary-300" mask="url(#mask-2)" fill="#9CA0A0" fill-rule="evenodd">
<g transform="translate(-3.000000, -3.000000)" id="Rectangle-6">
<rect x="0" y="0" width="30" height="30" rx="2"/>
</g>
</g>
</g>
</g>
</g>
</svg>
问题是,use
和mask
标记使用的ID不是唯一的,在我得到的另一个SVG中可能相同。
有办法解决这个问题吗?我对该领域一无所知,所以我可以使用你的帮助。 (或自动工具)
答案 0 :(得分:2)
尝试使用我制作的名为svgood的npm软件包。它将删除 defs 和 g (它们倾向于包含 mask s)元素,并将 path 元素浮动到 svg 的顶层。完成此操作后,您可以自己将样式和变换直接应用于svg,这往往比使用遮罩工作要容易得多,并且可以为您提供更多控制。我有一些计划使其更有用,但请告知我是否可以添加一些使其更适合您的用例的东西。
答案 1 :(得分:1)
这是一个重复数据删除功能。
注意:这是相当激进的,因为它会完全删除需要<svg>
替换的id
内部元素的任何绑定事件,因为我是将他们的outerHTML
视为字符串,在需要时替换id
并用结果字符串替换outerHTML
的整个<svg>
。在<svg>
内部标签上绑定事件将是不寻常的,非常罕见,但并非不可能,因此我的警告。我也懒得用jQuery编写它,因为它会花更多的香草,至少对我而言。
function dedupeIDs() {
let allSVGs = $('svg'),
allIDs = [];
allSVGs.each((i, e) => {
let outer = e.outerHTML;
$('[id]', e).each((o, k) => {
let unique = k.id;
if (allIDs.indexOf(k.id) > -1) {
unique = createUniqueId(k.id, allIDs);
outer = outer.replace('"#' + k.id + '"', '"#' + unique + '"')
.replace('"' + k.id + '"', '"' + unique + '"');
}
allIDs.push(unique);
})
if (e.outerHTML !== outer) {
e.outerHTML = outer;
};
})
function createUniqueId(id, ids) {
let i = 0;
while (++i && ids.indexOf(id.replace(/\d+/g, '') + i) > -1);
return id.replace(/\d+/g, '') + i;
}
}
$(window).load(dedupeIDs);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16px" height="16px" viewBox="0 0 16 16" version="1.1">
<!-- Generator: Sketch 49 (51002) - http://www.bohemiancoding.com/sketch -->
<title>icons/Search</title>
<desc>Created with Sketch.</desc>
<defs>
<path d="M19.9540595,19.3616366 L15.8890991,15.2679046 C18.3035819,12.5133747 18.0987254,8.33903567 15.4261496,5.83443006 C12.7535738,3.32982445 8.57707245,3.39817204 5.98778844,5.98888691 C3.39850443,8.57960179 3.33019459,12.7584111 5.8334169,15.4324638 C8.33663921,18.1065165 12.5086727,18.3114862 15.2616813,15.8956692 L19.3619892,19.9540341 C19.5318715,20.0382483 19.7365147,20.0046375 19.8705748,19.8705033 C20.0046349,19.7363691 20.0382272,19.5316129 19.9540595,19.3616366 Z M10.7813881,16.7091105 C7.53099588,16.7091105 4.8960324,14.0726909 4.8960324,10.8205025 C4.8960324,7.56831416 7.53099588,4.93189457 10.7813881,4.93189457 C14.0317802,4.93189457 16.6667437,7.56831416 16.6667437,10.8205025 C16.6571029,14.0520297 14.0464342,16.6720327 10.8167355,16.691427 L10.7813881,16.7091105 Z" id="path-1"/>
</defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Artboard" transform="translate(-704.000000, -382.000000)">
<g id="icons/Search" transform="translate(700.000000, 378.000000)">
<mask id="mask-2" fill="white">
<use xlink:href="#path-1"/>
</mask>
<use id="Search" fill="#9CA0A0" fill-rule="evenodd" xlink:href="#path-1"/>
<g id="mixin/primary-300" mask="url(#mask-2)" fill="#9CA0A0" fill-rule="evenodd">
<g transform="translate(-3.000000, -3.000000)" id="Rectangle-6">
<rect x="0" y="0" width="30" height="30" rx="2"/>
</g>
</g>
</g>
</g>
</g>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16px" height="16px" viewBox="0 0 16 16" version="1.1">
<!-- Generator: Sketch 49 (51002) - http://www.bohemiancoding.com/sketch -->
<title>icons/Search</title>
<desc>Created with Sketch.</desc>
<defs>
<path d="M19.9540595,19.3616366 L15.8890991,15.2679046 C18.3035819,12.5133747 18.0987254,8.33903567 15.4261496,5.83443006 C12.7535738,3.32982445 8.57707245,3.39817204 5.98778844,5.98888691 C3.39850443,8.57960179 3.33019459,12.7584111 5.8334169,15.4324638 C8.33663921,18.1065165 12.5086727,18.3114862 15.2616813,15.8956692 L19.3619892,19.9540341 C19.5318715,20.0382483 19.7365147,20.0046375 19.8705748,19.8705033 C20.0046349,19.7363691 20.0382272,19.5316129 19.9540595,19.3616366 Z M10.7813881,16.7091105 C7.53099588,16.7091105 4.8960324,14.0726909 4.8960324,10.8205025 C4.8960324,7.56831416 7.53099588,4.93189457 10.7813881,4.93189457 C14.0317802,4.93189457 16.6667437,7.56831416 16.6667437,10.8205025 C16.6571029,14.0520297 14.0464342,16.6720327 10.8167355,16.691427 L10.7813881,16.7091105 Z" id="path-1"/>
</defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Artboard" transform="translate(-704.000000, -382.000000)">
<g id="icons/Search" transform="translate(700.000000, 378.000000)">
<mask id="mask-2" fill="white">
<use xlink:href="#path-1"/>
</mask>
<use id="Search" fill="#9CA0A0" fill-rule="evenodd" xlink:href="#path-1"/>
<g id="mixin/primary-300" mask="url(#mask-2)" fill="#9CA0A0" fill-rule="evenodd">
<g transform="translate(-3.000000, -3.000000)" id="Rectangle-6">
<rect x="0" y="0" width="30" height="30" rx="2"/>
</g>
</g>
</g>
</g>
</g>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16px" height="16px" viewBox="0 0 16 16" version="1.1">
<!-- Generator: Sketch 49 (51002) - http://www.bohemiancoding.com/sketch -->
<title>icons/Search</title>
<desc>Created with Sketch.</desc>
<defs>
<path d="M19.9540595,19.3616366 L15.8890991,15.2679046 C18.3035819,12.5133747 18.0987254,8.33903567 15.4261496,5.83443006 C12.7535738,3.32982445 8.57707245,3.39817204 5.98778844,5.98888691 C3.39850443,8.57960179 3.33019459,12.7584111 5.8334169,15.4324638 C8.33663921,18.1065165 12.5086727,18.3114862 15.2616813,15.8956692 L19.3619892,19.9540341 C19.5318715,20.0382483 19.7365147,20.0046375 19.8705748,19.8705033 C20.0046349,19.7363691 20.0382272,19.5316129 19.9540595,19.3616366 Z M10.7813881,16.7091105 C7.53099588,16.7091105 4.8960324,14.0726909 4.8960324,10.8205025 C4.8960324,7.56831416 7.53099588,4.93189457 10.7813881,4.93189457 C14.0317802,4.93189457 16.6667437,7.56831416 16.6667437,10.8205025 C16.6571029,14.0520297 14.0464342,16.6720327 10.8167355,16.691427 L10.7813881,16.7091105 Z" id="path-1"/>
</defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Artboard" transform="translate(-704.000000, -382.000000)">
<g id="icons/Search" transform="translate(700.000000, 378.000000)">
<mask id="mask-2" fill="white">
<use xlink:href="#path-1"/>
</mask>
<use id="Search" fill="#9CA0A0" fill-rule="evenodd" xlink:href="#path-1"/>
<g id="mixin/primary-300" mask="url(#mask-2)" fill="#9CA0A0" fill-rule="evenodd">
<g transform="translate(-3.000000, -3.000000)" id="Rectangle-6">
<rect x="0" y="0" width="30" height="30" rx="2"/>
</g>
</g>
</g>
</g>
</g>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16px" height="16px" viewBox="0 0 16 16" version="1.1">
<!-- Generator: Sketch 49 (51002) - http://www.bohemiancoding.com/sketch -->
<title>icons/Search</title>
<desc>Created with Sketch.</desc>
<defs>
<path d="M19.9540595,19.3616366 L15.8890991,15.2679046 C18.3035819,12.5133747 18.0987254,8.33903567 15.4261496,5.83443006 C12.7535738,3.32982445 8.57707245,3.39817204 5.98778844,5.98888691 C3.39850443,8.57960179 3.33019459,12.7584111 5.8334169,15.4324638 C8.33663921,18.1065165 12.5086727,18.3114862 15.2616813,15.8956692 L19.3619892,19.9540341 C19.5318715,20.0382483 19.7365147,20.0046375 19.8705748,19.8705033 C20.0046349,19.7363691 20.0382272,19.5316129 19.9540595,19.3616366 Z M10.7813881,16.7091105 C7.53099588,16.7091105 4.8960324,14.0726909 4.8960324,10.8205025 C4.8960324,7.56831416 7.53099588,4.93189457 10.7813881,4.93189457 C14.0317802,4.93189457 16.6667437,7.56831416 16.6667437,10.8205025 C16.6571029,14.0520297 14.0464342,16.6720327 10.8167355,16.691427 L10.7813881,16.7091105 Z" id="path-1"/>
</defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Artboard" transform="translate(-704.000000, -382.000000)">
<g id="icons/Search" transform="translate(700.000000, 378.000000)">
<mask id="mask-2" fill="white">
<use xlink:href="#path-1"/>
</mask>
<use id="Search" fill="#9CA0A0" fill-rule="evenodd" xlink:href="#path-1"/>
<g id="mixin/primary-300" mask="url(#mask-2)" fill="#9CA0A0" fill-rule="evenodd">
<g transform="translate(-3.000000, -3.000000)" id="Rectangle-6">
<rect x="0" y="0" width="30" height="30" rx="2"/>
</g>
</g>
</g>
</g>
</g>
</svg>
个人和主观的注意事项:我的意思是更像是一个概念验证,我个人不会在生产环境中使用它。如果必须,我宁愿让代码只运行一次并手动将页面源代码中的每个<svg>
的结果放在源代码中的<svg>
内,这样我就会有独特的从那时起id
,无需运行重复数据删除功能。
恕我直言,您的问题在SO上的界限太宽泛 如果您需要更精致或更具侵略性的东西,您应该考虑聘请专业人士。即使您自己是编码员,不了解编程语言也不足以让陌生人为您编码。按照SO标准,您至少应该尝试当前请求的功能,而您没有。没有人,在当前问题的范围内,你是一个要求免费工作的客户。这听起来并不像你在这里学习,提高你的编码技巧,试图理解一个概念 如果这听起来很苛刻,请接受我的道歉,我希望它会证明是有用的,并且会帮助你在编码和寻求帮助方面做得更好。这是一个原则。我不苦,也不是个人,也不是不安。快乐的编码!