在输入类型图像中更改svg中的填充颜色

时间:2018-03-20 14:10:40

标签: javascript jquery html css svg

我正在关注这个例子: img src SVG changing the fill color

要更改输入标签,请执行以下操作:

<input name="ctl00$ctl00$cphM$cph$ImageButtonAnadirLote" class="btnAfirmativo" id="ctl00_ctl00_cphM_cph_ImageButtonAnadirLote" style="border-width: 0px; cursor: pointer;" onclick='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl00$cphM$cph$ImageButtonAnadirLote", "", true, "", "", false, false))' type="image" src="https://upload.wikimedia.org/wikipedia/commons/f/f8/Windows_logo_-_2012_%28red%29.svg">

CSS的位置是:

btnAfirmativo{width: 350px; height: 350px;}
btnAfirmativo path {fill: #000 !important;}

更改输入类型图像引用的svg的颜色。我尝试在此处修改代码:http://jsfiddle.net/kurnosem/fucdt3fp/2/

还有一个问题:Styling SVG Fill Color of an input element 但是这个没有提供我想要的解决方案。

如果图像是svg文件,是否可以更改输入标签类型图像的颜色? (改编jsfiddle代码)

1 个答案:

答案 0 :(得分:1)

由于您已经打开SVG并操纵其内容,因此您也可以使用其填充颜色。

var $path = jQuery(data).find('path');
$path.attr('style', $path.attr('style').replace("#e81123","#000"));

Updated fiddle

Stack snippet

$(function(){
		//jQuery('img.btnAfirmativo').each(function(){
    jQuery('input.btnAfirmativo').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) {
        
            var $path = jQuery(data).find('path');
            $path.attr('style', $path.attr('style').replace("#e81123","#000"));

            // 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');
            
            // Check if the viewport is set, else we gonna set it if we can.
            if(!$svg.attr('viewBox') && $svg.attr('height') && $svg.attr('width')) {
                $svg.attr('viewBox', '0 0 ' + $svg.attr('height') + ' ' + $svg.attr('width'))
            }
    
            // Replace image with new SVG
            $img.replaceWith($svg);
    
        }, 'xml');
    
    });
});
btnAfirmativo{width: 350px; height: 350px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<img class="btnAfirmativo" src="https://upload.wikimedia.org/wikipedia/commons/f/f8/Windows_logo_-_2012_%28red%29.svg" alt="Microsoft" width="350" height="350">
<input name="ctl00$ctl00$cphM$cph$ImageButtonAnadirLote" class="btnAfirmativo" id="ctl00_ctl00_cphM_cph_ImageButtonAnadirLote" style="border-width: 0px; cursor: pointer;" onclick='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl00$cphM$cph$ImageButtonAnadirLote", "", true, "", "", false, false))' type="image" src="https://upload.wikimedia.org/wikipedia/commons/f/f8/Windows_logo_-_2012_%28red%29.svg">

使用CSS,将其更新为此

.btnAfirmativo{width: 350px; height: 350px;}
.btnAfirmativo + svg path {fill: #0f0 !important;}

Stack snippet

$(function(){
		//jQuery('img.btnAfirmativo').each(function(){
    jQuery('input.btnAfirmativo').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');
            
            // Check if the viewport is set, else we gonna set it if we can.
            if(!$svg.attr('viewBox') && $svg.attr('height') && $svg.attr('width')) {
                $svg.attr('viewBox', '0 0 ' + $svg.attr('height') + ' ' + $svg.attr('width'))
            }
    
            // Replace image with new SVG
            $img.replaceWith($svg);
    
        }, 'xml');
    
    });
});
.btnAfirmativo{width: 350px; height: 350px;}
.btnAfirmativo + svg path {fill: #0f0 !important;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<img class="btnAfirmativo" src="https://upload.wikimedia.org/wikipedia/commons/f/f8/Windows_logo_-_2012_%28red%29.svg" alt="Microsoft" width="350" height="350">

<input name="ctl00$ctl00$cphM$cph$ImageButtonAnadirLote" class="btnAfirmativo" id="ctl00_ctl00_cphM_cph_ImageButtonAnadirLote" style="border-width: 0px; cursor: pointer;" onclick='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl00$cphM$cph$ImageButtonAnadirLote", "", true, "", "", false, false))' type="image" src="https://upload.wikimedia.org/wikipedia/commons/f/f8/Windows_logo_-_2012_%28red%29.svg">