我有一个data-src需要迭代的元素数组。我将通过源 url 请求每个图像的 svg 图标数据。然后img用svg.
我已经尝试过 $.get 和 $.ajax。他们以同样时髦的方式工作。
$($('#container img').each(function(){
var $img = $(this);
var imgURL = $img.data('src');
$.ajax({url: imgURL}).done(function(data) {
// Get the SVG tag, ignore the rest
var $svg = $(data).find('svg');
// Remove any invalid XML tags as per http://validator.w3.org
$svg = $svg.removeAttr('xmlns:a');
// Replace image with new SVG
$img.replaceWith($svg);
});
}));
我希望每个都img应该替换为svg. 有 6 个不同的图像元素需要迭代。但它只渲染 3 种类型的图像——另外 3 种是重复的。
编辑
根据需要,我添加了重复 3 次的 svg 图标的 HTML。id="icon/general/path"除了附加到的ids(即此处)之外<g>,其他所有内容都是相同的。
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="80px" height="80px" viewBox="0 0 30 30" version="1.1">
<title>icon/general/path</title>
<desc>Created with Sketch.</desc>
<defs>
<path d="M23.35,17.85 C26.1811218,17.9531502 28.4064642,20.3075624 28.35,23.14 C28.4175618,25.9801499 26.1890783,28.3467994 23.35,28.45 L8.5,28.45 L3.5,22.7 C0.861206245,19.9120615 0.861206245,15.5479385 3.5,12.76 C4.82392582,11.4296085 6.62310903,10.6811483 8.5,10.68 L8.58,10.68 C10.456891,10.6811483 12.2560742,11.4296085 13.58,12.76 C16.2517576,15.5579617 16.2517576,19.9620383 13.58,22.76 L10.72,26 L23.35,26 C24.8381231,25.9193249 25.9794604,24.6481884 25.9,23.16 C25.9624275,21.6832523 24.8258567,20.4307959 23.35,20.35 L19.73,20.35 C17.8398194,20.249715 16.3586134,18.6878391 16.3586134,16.795 C16.3586134,14.9021609 17.8398194,13.340285 19.73,13.24 L21.07,13.24 L21.07,15.74 L19.73,15.74 C19.2152683,15.8271071 18.8385357,16.2729498 18.8385357,16.795 C18.8385357,17.3170502 19.2152683,17.7628929 19.73,17.85 L23.35,17.85 Z M5.35,21.07 L8.52,24.68 L11.71,21.08 C13.4566172,19.2589987 13.4829132,16.3927401 11.77,14.54 C10.9228684,13.6811215 9.76635871,13.1983418 8.56,13.2 L8.5,13.2 C7.31447448,13.2142619 6.18247682,13.6958101 5.35,14.54 C3.62163028,16.3735661 3.62163028,19.2364339 5.35,21.07 Z M23.11,8 C22.7594008,8.01423712 22.4175606,7.88829599 22.16,7.65 C21.6579442,7.14330328 21.6579442,6.32669672 22.16,5.82 C22.4038589,5.57573672 22.7348454,5.438477 23.08,5.438477 C23.4251546,5.438477 23.7561411,5.57573672 24,5.82 C24.2451774,6.04938214 24.3920996,6.36472746 24.41,6.7 C24.41,7.41797017 23.8279702,8 23.11,8 Z M23.09,15 L19.4,10.61 C17.4037602,8.50077083 17.4037602,5.19922917 19.4,3.09 C20.4010694,2.083572 21.76049,1.51534855 23.18,1.51 C24.6131548,1.50581957 25.9885201,2.07468792 27,3.09 C29.0025573,5.20057413 29.0025573,8.50942587 27,10.62 L23.09,15 Z M21.09,4.87 L21.09,4.85 C19.9927338,6.02355371 20.014778,7.85322326 21.14,9 L23.08,11.24 L25.08,9 C26.1812291,7.84509836 26.1856235,6.0302209 25.09,4.87 C23.9805846,3.77719207 22.1994154,3.77719207 21.09,4.87 Z M8.53,19.41 C7.79081922,19.4051708 7.12628916,18.9584612 6.8427573,18.2758037 C6.55922544,17.5931462 6.71174316,16.8070877 7.23,16.28 C7.81627484,15.6837354 8.73089122,15.5577824 9.45651146,15.9733844 C10.1821317,16.3889863 10.5362598,17.2416179 10.3185891,18.049002 C10.1009183,18.8563862 9.36619368,19.415463 8.53,19.41 Z" id="path-1"></path>
</defs>
<g id="icon/general/path" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<mask id="mask-2" fill="white">
<use xlink:href="#path-1"></use>
</mask>
<use id="Mask" fill="#282B3E" fill-rule="nonzero" xlink:href="#path-1"></use>
<g id="-color/navy" mask="url(#mask-2)" fill="#282B3E" fill-rule="nonzero">
<rect id="-navy" x="0" y="0" width="30" height="30"></rect>
</g>
</g>
</svg>