通过jquery更改svg中的文本(文本是动态值 - 温度)

时间:2013-05-08 17:59:05

标签: jquery jquery-svg

我想更改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-Installation的温度 我尝试过以下代码,但没有任何反应:

   <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] );
]]>

以$('#VL_temp'开头的行的问题是什么?其余的代码似乎没问题,因为这部分在另一个svg中使用jquery(svn-link to the svg

2 个答案:

答案 0 :(得分:5)

<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');

答案 1 :(得分:-1)

我能够通过选择这样的id来改变它:

&#13;
&#13;
$('#VL_temp').text("replaced");
&#13;
<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>
&#13;
&#13;
&#13;