简体   繁体   English

抓取文本标签内容,并使用javascript将其替换为自定义文本

[英]Grab text tag content and replace it with custom text using javascript

Hi Below is my svg section 您好以下是我的svg部分

<g transform="translate(86.91003147398621,-21.306040772755345)" class="nv-label">
    <rect style="fill-opacity: 0; stroke-opacity: 0;"></rect>
    <text dy=".35em" style="fill-opacity: 1; fill: rgb(85, 85, 85); text-anchor: start;">Test Content</text>
</g>

Here, I need to get the text "Test Content" and replace it with some number, how can I do this with javascript? 在这里,我需要获取文本“ Test Content”并将其替换为一些数字,该如何使用javascript做到这一点?

Any help would be appreciated. 任何帮助,将不胜感激。

As you have mentioned there are multiple g tags, you can use document.querySelectorAll('g text')[1]; 如前所述,有多个g标签,可以使用document.querySelectorAll('g text')[1]; to get the element reference of second text inside g tag and then change the value using textContent . 获取g标签内第二个text的元素引用,然后使用textContent更改值。 (answer updated based on OP comment) (答案根据OP评论进行了更新)

 var textElem = document.querySelectorAll('g text')[1]; textElem.textContent = 1111; 
 <g transform="translate(86.91003147398621,-21.306040772755345)" class="nv-label"> <rect style="fill-opacity: 0; stroke-opacity: 0;"></rect> <text dy=".35em" style="fill-opacity: 1; fill: rgb(85, 85, 85); text-anchor: start;">Test Content</text> </g> <br/> <g transform="translate(86.91003147398621,-21.306040772755345)" class="nv-label"> <rect style="fill-opacity: 0; stroke-opacity: 0;"></rect> <text dy=".35em" style="fill-opacity: 1; fill: rgb(85, 85, 85); text-anchor: start;">Test Content</text> </g> <br/> <g transform="translate(86.91003147398621,-21.306040772755345)" class="nv-label"> <rect style="fill-opacity: 0; stroke-opacity: 0;"></rect> <text dy=".35em" style="fill-opacity: 1; fill: rgb(85, 85, 85); text-anchor: start;">Test Content</text> </g> 

This could help you, take a look :D 这可以帮助您,看看:D

 function changeText(){ var svg = document.querySelector('g'); svg.lastElementChild.innerText = "hello World" } changeText(); 
 <g transform="translate(86.91003147398621,-21.306040772755345)" class="nv-label"> <rect style="fill-opacity: 0; stroke-opacity: 0;"></rect> <text dy=".35em" style="fill-opacity: 1; fill: rgb(85, 85, 85); text-anchor: start;">Test Content</text> </g> 

What I think many other answers are neglecting to give you is advice on how to set up your SVG so that you can easily find your text element later. 我认为许多其他答案都没有给您提供有关如何设置SVG的建议,以便您以后可以轻松找到文本元素。

The normal way to do that is to give the text element an id attribute. 通常的方法是给text元素一个id属性。 Then later you can find it by calling getElementById() . 然后,您可以通过调用getElementById()找到它。

 var textElem = document.getElementById("mytext"); textElem.textContent = "1234"; 
 <svg> <text id="mytext" y="100">Test Content</text> </svg> 

There are other ways you could mark your element. 还有其他方式可以标记元素。 You could do it by class : 您可以按class做:

 var allTextElems = document.getElementsByClassName("mytext"); for (var i=0; i< allTextElems.length; i++) { allTextElems.item(i).textContent = "1234"; }; 
 <svg> <text class="mytext" y="75">Test Content</text> <text class="notmytext" y="100">Test Content</text> <text class="mytext" y="125">Test Content</text> </svg> 

Or you could do it using a data attribute: 或者您可以使用data属性来执行此操作:

 var allTextElems = document.querySelectorAll('[data-key=mytext]'); allTextElems.forEach(function(item) { item.textContent = "1234"; }); 
 <svg> <text data-key="mytext" y="75">Test Content</text> <text data-key="notmytext" y="100">Test Content</text> <text data-key="mytext" y="125">Test Content</text> </svg> 

If you can't modify the SVG for some reason, then the next obvious solution would be to locate the text element using its position in the DOM tree relative to other elements. 如果由于某种原因无法修改SVG,那么下一个明显的解决方案是使用文本元素在DOM树中相对于其他元素的位置来定位文本元素。

 // Select the first text element in the group which has class="two" var textElem = document.querySelector("g.two text:first-child"); textElem.textContent = "1234"; 
 <svg> <g class="one"> <text y="50">Text in group 1</text> </g> <g class="two"> <text id="mytext" y="100">Test Content</text> <text y="125">Other text in group 2</text> </g> </svg> 

If you don't know where in the file the target text will be, you only know what the text contains, then you would need to search all the text elements to find the one with the correct text. 如果您不知道目标文本在文件中的位置,则只知道该文本包含的内容,那么您将需要搜索所有文本元素以找到具有正确文本的元素。

 // Select the SVG we want to search // (This is an example of what you would need to do if you had more than one svg on the page) var mySvg = document.querySelector("#mysvg"); // Find all the text elements in that svg var allTextElems = document.querySelectorAll("text"); // Check all the text elements found, and if they contain our text // then replace it with the new text. allTextElems.forEach(function(item) { if (item.textContent === "Test Content") { item.textContent = "1234"; } }); 
 <svg id="mysvg"> <g class="one"> <text y="50">Text in group 1</text> </g> <g class="two"> <text id="mytext" y="100">Test Content</text> <text y="125">Other text in group 2</text> </g> </svg> 

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

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