简体   繁体   English

使用HTML文本字段沿SVG textPpath更新文本

[英]Using HTML text-field to update text along an SVG textPpath

I have a textfield and am trying to pass its text along to an SVG textPath . 我有一个textfield,正试图将其文本传递给SVG textPath

At first I thought I could use an ID to update the text inside of the textPath, but the SVG does not seem to recognize the HTML. 起初我以为可以使用ID更新textPath内的文本,但是SVG似乎无法识别HTML。 So in my searches I've encountered conversations about using HTMLinner(?) and foreign objects(?) to handle this, but no real examples. 因此,在搜索中,我遇到了有关使用HTMLinner(?)和外来对象(?)来处理此问题的对话,但没有真正的示例。

Here's my work so far. 到目前为止,这是我的工作。

 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> 

jsFiddle : https://jsfiddle.net/2pr8evoe/2/ jsFiddlehttps : //jsfiddle.net/2pr8evoe/2/

The SVG text element doesn't have innerHtml , it's textContent that you want to use SVG文本元素没有innerHtml ,而是要使用的textContent

lblValue.textContent = s;

https://jsfiddle.net/2pr8evoe/3/ 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> 

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

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