简体   繁体   English

更改复杂 SVG 中的文本

[英]Changing text inside a complicated SVG

I have a small SVG illustration of a flip calendar.我有一个翻盖日历的小 SVG 插图。 I've added a text field that currently says "number" in it.我添加了一个当前显示“数字”的文本字段。 I'm trying to display how many days have passed since a specific date, but since every day it increases by one, I want to have an active script updating the number on my website.我试图显示自特定日期以来已经过去了多少天,但由于它每天增加一,我希望有一个活动脚本更新我网站上的数字。 The illustration was made on Adobe Draw, so the strokes are rather lengthy and confusing, so I removed all of the illustration parts of the SVG for clarity - inside the SVG tags is only the text field.插图是在 Adob​​e Draw 上制作的,所以笔画相当冗长且令人困惑,所以为了清晰起见,我删除了 SVG 的所有插图部分 - SVG 标签内只有文本字段。

I think I am missing a key point about how to change the innerText for a text field.我想我错过了一个关于如何更改文本字段的innerText 的关键点。 On examples I've found on MDN web docs, you can use the DOM to change attributes inside an SVG, but I have no luck.在我在 MDN 网络文档上找到的示例中,您可以使用 DOM 来更改 SVG 内的属性,但我没有运气。 I have tried:我试过了:

   document.getQuery(".calendar").innerText = diffDays; 
   document.getElementsByClass("calendar").innerText = diffDays;
   document.getElementById("journaled").innerText = diffDays;

 <svg id="test" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 99 75.5"> <script type="text/javascript"> const start = new Date('5/23/2013'); const today = new Date(); const diffTime = Math.abs(today - start); const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); document.getQuery(".calendar").textContent = "is anything working?"; document.getElementsByClassName("calendar").textContent = diffDays; document.getElementById("journaled").textContent = diffDays; </script> <defs> <style> .calendar { font-size: 12px; font-family: Georgia; } </style> </defs> <text contentEditable="true" class="calendar" id="journaled" transform="translate(20 51.63)">number</text> </svg>

I have also researched the problem that displays in the console, "Uncaught TypeError: document.getQuery is not a function" and I can't find any resources relevant to SVGs.我还研究了控制台中显示的问题,“Uncaught TypeError: document.getQuery is not a function”,但我找不到任何与 SVG 相关的资源。 Any help would be greatly appreciated.任何帮助将不胜感激。 JSFiddle JSFiddle

I removed getQuery and getElementsByClassName lines我删除了getQuerygetElementsByClassName

and extract your JavaScript outside svg and it works ... try this :在 svg 之外提取你的 JavaScript 并且它可以工作......试试这个:

 const start = new Date('5/23/2013'); const today = new Date(); const diffTime = Math.abs(today - start); const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); document.getElementById("journaled").textContent = diffDays;
 <svg id="test" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 99 75.5"> <defs> <style> .calendar { font-size: 12px; font-family: Georgia; } </style> </defs> <text contentEditable="true" class="calendar" id="journaled" transform="translate(20 51.63)">number</text> </svg>

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

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