繁体   English   中英

如何使用jQuery动态更改SVG圆填充颜色的颜色

[英]How to change the color of an SVG circle fill color using jQuery dynamically

我想动态更改SVG元素的圆圈的颜色。 如何根据AJAX通话响应中的值动态更改圆圈的颜色?

HTML:

<tr>
  <td style="width: 33.3333%; text-align: center;">
    <svg height="100" width="100">
      <circle id="parking_slot" cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="green" />
    </svg>
  </td>
</tr>

JavaScript:

$(document).ready(function() {
  $.ajax({
    url: 'url',
    type: 'GET',
    data: {
      format: 'json'
    },
    success: function(response) {
      if (response.field1 < 50) {
        $("#field1").text("car is parked");
        $("#parking_slot").fill("red");
      } else {
        $("#field1").text("car is not parked");
        $("#parking_slot").fill("green");
      }
    },
    error: function() {
      $('#errors').text("There was an error processing your request. Please try again.");
    }
  });
});

像这样使用:

$("#parking_slot").css("background-color", "red");

暂无
暂无

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

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