изменение текста в svg с помощью jquery (текст является динамическим значением - температура)

Я хотел бы изменить эту текстовую часть svg:

<text id="VL_temp" xml:space="preserve" style="font-size:40px;font-style:normal;font-weight:normal;line-height:100%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans;font-stretch:normal;font-variant:normal;text-anchor:start;text-align:start;writing-mode:lr" x="432.24234" y="110" sodipodi:linespacing="100%">**--**</text>

Я хотел бы отобразить температуру, полученную из установки KNX, которую я пробовал со следующим кодом, но ничего не происходит:

   <script>
<![CDATA[
var thisGA = '14/0/4';
var thisTransform = 'DPT:9.001';
visu = new CometVisu('/cgi-bin/');
visu.update = function ( json )
{
  var temp = Transform[thisTransform].decode( json[thisGA] );
  $('#VL_temp', svg.root()).text('temp');
}
$(window).unload(function() {
  visu.stop();
});
visu.user = 'demo_user';
visu.subscribe( [thisGA] );
]]>

whats the problem with the line starting with $('#VL_temp'? The rest of the code seems to be ok, because this part works in another svg with jquery (svn-link to the svg)


person jensgulow    schedule 08.05.2013    source источник


Ответы (2)


Тег <text> в SVG должен содержать тег <tspan>, а текст должен быть в теге <tspan>, например:

<text id="VL_temp" xml:space="preserve" style="" x="432.24234" y="110" sodipodi:linespacing="100%"><tspan>**--**</tspan></text>

Код также необходимо обновить:

$('#VL_temp tspan', svg.root()).text('temp');
person hegemon    schedule 08.05.2013

Я смог изменить его, просто выбрав идентификатор следующим образом:

$('#VL_temp').text("replaced");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <svg>
<text id="VL_temp" xml:space="preserve" style="font-size:40px;font-style:normal;font-weight:normal;line-height:100%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans;font-stretch:normal;font-variant:normal;text-anchor:start;text-align:start;writing-mode:lr" dy="1em" sodipodi:linespacing="100%">hello world</text>
</svg>

person Claytronicon    schedule 22.08.2016