2016-10-31 5 views
0

私は今のところよく見える問題の答えを見つけようと2時間ほどインターネットを閲覧していますが、問題に適切な解決策が見つけられません。 私はsvgファイルのテキストを変更する必要があります。最終的にはブラウザのテキスト入力になるでしょう。私はforeignobjectを使用することができましたが、最終的には私の問題の解決策ではありません。なぜなら、私はそれをパスに合わせる必要があるからです。同様の質問へjqueryでsvgテキストを変更する

マイSVG

<?xml version="1.0" encoding="utf-8"?> 
<!-- Generator: Adobe Illustrator 20.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> 
<svg version="1.1" id="Ebene_1" xmlns="http://www.w3.org/2000/svg"  xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" 
viewBox="0 0 841.9 595.3" style="enable-background:new 0 0 841.9 595.3;" xml:space="preserve"> 
<style type="text/css"> 
</style> 
<text transform="matrix(1 0 0 1 325.2451 144.7144)" class="st0 st1" id="id-of-the-text">hey</text> 
<text class="testText" id="testText" x="10" y="20" style="fill:red;">Several lines: 
    <tspan x="10" y="45">First line.</tspan> 
    <tspan x="10" y="70">Second line.</tspan> 
    </text> 
    <text x="40" y="60">more text</text> 
</svg> 

と回答から失敗

$('#id-of-the-text').textContent = 'test'; 
$("#id-of-the-text").text("new-value"); 
$("#id-of-the-text")['innerText' in $("#id-of-the-text") ? "innerText" : "textContent"] = "some value"; 

それは何も私のために働かない、なぜそうでない場合、私は理解できない、愚かな小さなミスかもしれません。

+1

あなたのIDは 'ID-の-text'、ない' testText'です。テキスト( '新しいテキスト'); 'は動作します:https://jsfiddle.net/5gfcym6p/ –

+0

もう2つの異なるテキスト要素があります。それにもかかわらず、前の結果と同じ結果を返しました –

答えて

0

text()を使用すると効果があります。あなたの最初の行にエラーがあったために失敗した可能性があります。

いずれの場合でも、テキストを変更するさまざまな方法については、私の例を参照してください。 jQueryとDOMの機能が混在しているものがあります。

// Change the first text element 
 
$('#id-of-the-text').text("hey 2"); 
 

 
// Change the first text node of the second text element. 
 
// Have to do this a little differently. We need to be careful that we only change 
 
// the first text node and that we don't replace everything including the tspans. 
 
$("#testText").get(0).firstChild.textContent = "Several lines 2"; 
 

 
// Change the first tspan 
 
$("#testText tspan:nth-child(1)").text("First line 2"); 
 

 
// Change the second tspan 
 
// Alternative to using "nth-child(2)": 
 
$("#testText tspan").last().text("Second line 2"); 
 

 
// Change the last text element 
 
$("svg text").last().text("more text 2");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<svg version="1.1" id="Ebene_1" xmlns="http://www.w3.org/2000/svg"  xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" 
 
viewBox="0 0 841.9 595.3" style="enable-background:new 0 0 841.9 595.3;" xml:space="preserve"> 
 
    <style type="text/css"> 
 
    </style> 
 

 
    <text transform="matrix(1 0 0 1 325.2451 144.7144)" class="st0 st1" id="id-of-the-text">hey</text> 
 
    <text class="testText" id="testText" x="10" y="20" style="fill:red;">Several lines: 
 
    <tspan x="10" y="45">First line.</tspan> 
 
    <tspan x="10" y="70">Second line.</tspan> 
 
    </text> 
 
    <text x="40" y="60">more text</text> 
 
</svg>

+0

これは問題が何であるかを知りました。これは以前のコードと同様に動作しますが、svgが直接実装されている場合のみですhtml タグを使用しても問題が解決しない場合、それは問題です –

+0

また、「」を使用する際にもそれを行うことができます。しかし、オブジェクトの 'contentDocument'プロパティを介してSVG DOMにアクセスする必要があります。参照:http://stackoverflow.com/questions/11434916/javascript-accessing-inner-dom-of-svg –

+0

申し訳ありません、ありがとう、たくさん(y) –

関連する問題