Answer the question
In order to leave comments, you need to log in
How to modify the script so that it remembers the values and writes them to localstorage?
Background: started using SVg in a project. There are quite a lot of them on each page, but many of them are repeated several times. I couldn't write svg code in html, it was forbidden. And there were two options, either task for assembly (I use gulp ) or another way out. I stopped to import as a regular image, but in order for the browser to expand the svg code, I found the script. And it looks something like this
. And there is a script that helps the browser to expand the svg code (that is, the browser has a full svg code) (it was necessary to work with CSS)
The code itself:
<img src='path/to/svg' class='svg>
jQuery('img.svg').each(function(){
var $img = jQuery(this);
var imgID = $img.attr('id');
var imgClass = $img.attr('class');
var imgURL = $img.attr('src');
jQuery.get(imgURL, function(data) {
// Get the SVG tag, ignore the rest
var $svg = jQuery(data).find('svg');
// Add replaced image's ID to the new SVG
if(typeof imgID !== 'undefined') {
$svg = $svg.attr('id', imgID);
}
// Add replaced image's classes to the new SVG
if(typeof imgClass !== 'undefined') {
$svg = $svg.attr('class', imgClass+' replaced-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);
}, 'xml');
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question