2015-09-25 66 views
1

我有一個文本框,並試圖將其文本傳遞到an SVG textPath使用HTML文本字段沿SVG文本更新文本Ppath

起初我以爲我可以用一個ID來更新textPath裏面的文本,但是SVG似乎不能識別HTML。所以在我的搜索中,我遇到了關於使用HTMLinner(?)和外部對象(?)來處理這個問題的對話,但沒有真正的例子。

這是我迄今的工作。

function edValueKeyPress() { 
 
    var edValue = document.getElementById("edValue"); 
 
    var s = edValue.value; 
 

 
    var lblValue = document.getElementById("lblValue"); 
 
    lblValue.innerText = "" + s; 
 

 
    //var s = $("#edValue").val(); 
 
    //$("#lblValue").text(s);  
 
}
<!-- create text field --> 
 
<input id="edValue" type="text" onKeyPress="edValueKeyPress()" onKeyUp="edValueKeyPress()"> 
 
<br> 
 

 
<!-- update label with field --> 
 
<span id="lblValue"></span> 
 

 
<!-- SVG path --> 
 
<svg width="100%" height="100%" viewBox="0 0 1000 300" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> 
 
    <defs> 
 
    <path id="MyPath" d="M 100 200 
 
      C 200 100 300 0 400 100 
 
      C 500 200 600 300 700 200 
 
      C 800 100 900 100 900 100" /> 
 
    </defs> 
 

 
    <use xlink:href="#MyPath" fill="none" stroke="red" /> 
 

 
    <!--handle text along path --> 
 
    <text font-family="Verdana" font-size="42.5"> 
 
    <textPath xlink:href="#MyPath" id="lblValue"> 
 
     Form text should go here 
 
    </textPath> 
 
    </text> 
 

 
    <!-- Show outline of the viewport using 'rect' element --> 
 
    <rect x="1" y="1" width="998" height="298" fill="none" stroke="black" stroke-width="2" /> 
 
</svg>

的jsfiddlehttps://jsfiddle.net/2pr8evoe/2/

回答

0

SVG的文本元素沒有innerHtml,這是textContent要使用

lblValue.textContent = s; 

https://jsfiddle.net/2pr8evoe/3/

function edValueKeyPress() { 
 
    var edValue = document.getElementById("edValue"); 
 
    var s = edValue.value; 
 

 
    var lblValue = document.getElementById("lblValue"); 
 
    lblValue.textContent =s; 
 
}
<!-- create text field --> 
 
<input id="edValue" type="text" onKeyPress="edValueKeyPress()" onKeyUp="edValueKeyPress()"> 
 
<br> 
 

 
<!-- SVG path --> 
 
<svg width="100%" height="100%" viewBox="0 0 1000 300" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> 
 
    <defs> 
 
    <path id="MyPath" d="M 100 200 
 
      C 200 100 300 0 400 100 
 
      C 500 200 600 300 700 200 
 
      C 800 100 900 100 900 100" /> 
 
    </defs> 
 

 
    <use xlink:href="#MyPath" fill="none" stroke="red" /> 
 

 
    <!--handle text along path --> 
 
    <text font-family="Verdana" font-size="42.5"> 
 
    <textPath xlink:href="#MyPath" id="lblValue"> 
 
     Form text should go here 
 
    </textPath> 
 
    </text> 
 

 
    <!-- Show outline of the viewport using 'rect' element --> 
 
    <rect x="1" y="1" width="998" height="298" fill="none" stroke="black" stroke-width="2" /> 
 
</svg>

+0

謝謝你的快速反應,意味着很多! – aenean