简体   繁体   English

如何使用JQuery动态更改已加载的svg源样式

[英]How to change loaded svg source style dynamically with JQuery

I am having problem editting svg source code that was assigned to a variable after retrieved from an ajax request. 我从ajax请求中检索到后,在编辑分配给变量的svg源代码时遇到问题。

//my html
<div class="col col-md-12">
    <div class="my-svg" id="show-logo" >
</div>



//javascript
var obj = {category: 'SPORT'}   
$.ajax({
        url : '{{ route("getsvgs") }}',
        type: 'GET',
        data: obj,
        success : function(response, statut){
            var svg_img = response.logo;
            //the ajax request is going on successful and returning the 
            //above svg code.
          $('#show-logo').append(svg_img );
          localStorage._el = JSON.stringify(response);//store localstorage
        },

    error : function(response, statut, error){
        console.log(response);
    },

    complete : function(responce, statut){
        console.log('completed');
    }
});

from what you can see the svg is been appended to the div and showing. 从您可以看到的svg被附加到div并显示出来。 here is the svg code 这是svg代码

/* response.logo = <svg class="image-svg" id="svg-logo" data-name="Layer 2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 298.63 86.14">
    <title>left</title>
    <text class="svg-logo-name" transform="translate(63.81 65.42)"></text>
    <text class="svg-logo-slogan" transform="translate(66.36 81.64)">logo slogan goes here</text>
    <path class="frontcolor" d="M70.74,172.32c5.11,4,13.08,4.11,16.84,3.32a0.82,0.82,0,0,1-.15,0c-6.82-1.45-11.1-3.74-14.81-7.09s-6.09-7.69-7.55-14.05C65.06,154.5,62.42,165.76,70.74,172.32Z" transform="translate(-62.3 -148.4)"/>
    <path class="backcolor" d="M93.94,181.92c-14.44,4.56-25.63-7-25.63-7a0.24,0.24,0,0,1,.15,0c5.64,3.63,15.69,4.76,23.33,1.87,9.63-3.64,16.27-12.5,16.58-20.72C108.38,156.12,110,176.85,93.94,181.92Z" transform="translate(-62.3 -148.4)"/>
    <path class="frontcolor" d="M113.53,202.4c-6.95-6.67-17.78-6.8-22.9-5.49a0.71,0.71,0,0,1,.21,0c9.27,2.4,15.1,6.2,20.14,11.75s8.28,12.76,10.27,23.3C121.25,231.94,124.85,213.27,113.53,202.4Z" transform="translate(-62.3 -148.4)"/>
    <path class="backcolor" d="M82,186.47c19.63-7.56,34.84,11.53,34.84,11.53a0.28,0.28,0,0,1-.21,0c-7.67-6-21.34-7.9-31.72-3.1-13.09,6-22.12,20.72-22.54,34.35C62.36,229.24,60.17,194.89,82,186.47Z" transform="translate(-62.3 -148.4)"/>
    <ellipse class="frontcolor" cx="25.18" cy="9.3" rx="9.25" ry="9.3"/>
</svg> */ 

Now when trying to edit the svg like this nothing is happening. 现在,当尝试像这样编辑svg时,没有任何反应。

$(".svg-logo-name text").css("font-size", "48px");
$(".frontcolor text").css("fill", "#eeeeee");
$(".backcolor text").css("fill", "#ccddee");

Nothing is been applied. 什么都没有应用。 how can I achieved that? 我该如何实现? please. 请。

The selectors are wrong. 选择器错误。 All three of your selectors are looking for an element with a descendant element of text . 您的所有三个选择器都在寻找包含text的后代元素的元素。 None of your SVG elements have descendants besides the <svg> element itself. 除了<svg>元素本身之外,您的SVG元素都没有后代。

Remove text from your CSS selectors. 从CSS选择器中删除text Also, very important , you cannot run the jQuery that modifies the styles until the SVG markup has been added to the DOM (your DIV), otherwise it won't find those elements to modify. 同样, 非常重要 ,在将SVG标记添加到DOM(您的DIV)之前,您无法运行修改样式的jQuery,否则它将找不到要修改的元素。 Run the jQuery in the success or complete callbacks. 成功完成回调中运行jQuery。

 $( '.svg-logo-name' ).css( 'font-size', '48px' ); $( '.frontcolor' ).css( 'fill', '#eee' ); $( '.backcolor' ).css( 'fill', '#cde' ); // Above code would go here: // $.ajax( { // success: function ( response, status ) { // // $( '#show-logo' ).append( response.logo ); // // // Modify SVG. // $( '.svg-logo-name' ).css( 'font-size', '48px' ); // $( '.frontcolor' ).css( 'fill', '#eee' ); // $( '.backcolor' ).css( 'fill', '#cde' ); // // } // } ); // // Or it would go here: // $.ajax( { // complete: function ( xhr, status ) { // // // Modify SVG. // if ( 'success' === status ) { // // $( '.svg-logo-name' ).css( 'font-size', '48px' ); // $( '.frontcolor' ).css( 'fill', '#eee' ); // $( '.backcolor' ).css( 'fill', '#cde' ); // // } // // } // } ); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <svg class="image-svg" id="svg-logo" data-name="Layer 2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 298.63 86.14"> <title>left</title> <text class="svg-logo-name" transform="translate(63.81 65.42)"></text> <text class="svg-logo-slogan" transform="translate(66.36 81.64)">logo slogan goes here</text> <path class="frontcolor" d="M70.74,172.32c5.11,4,13.08,4.11,16.84,3.32a0.82,0.82,0,0,1-.15,0c-6.82-1.45-11.1-3.74-14.81-7.09s-6.09-7.69-7.55-14.05C65.06,154.5,62.42,165.76,70.74,172.32Z" transform="translate(-62.3 -148.4)"/> <path class="backcolor" d="M93.94,181.92c-14.44,4.56-25.63-7-25.63-7a0.24,0.24,0,0,1,.15,0c5.64,3.63,15.69,4.76,23.33,1.87,9.63-3.64,16.27-12.5,16.58-20.72C108.38,156.12,110,176.85,93.94,181.92Z" transform="translate(-62.3 -148.4)"/> <path class="frontcolor" d="M113.53,202.4c-6.95-6.67-17.78-6.8-22.9-5.49a0.71,0.71,0,0,1,.21,0c9.27,2.4,15.1,6.2,20.14,11.75s8.28,12.76,10.27,23.3C121.25,231.94,124.85,213.27,113.53,202.4Z" transform="translate(-62.3 -148.4)"/> <path class="backcolor" d="M82,186.47c19.63-7.56,34.84,11.53,34.84,11.53a0.28,0.28,0,0,1-.21,0c-7.67-6-21.34-7.9-31.72-3.1-13.09,6-22.12,20.72-22.54,34.35C62.36,229.24,60.17,194.89,82,186.47Z" transform="translate(-62.3 -148.4)"/> <ellipse class="frontcolor" cx="25.18" cy="9.3" rx="9.25" ry="9.3"/> </svg> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM