简体   繁体   中英

How to convert <text> (text tag) to anchor tag in jquery?

I have one <text> tag which contains plain text but I want to show a hyperlink instead of plain text.

      <svg width="800" height="600" class="overlay">
<g transform="translate(100,220)">
<path class="link" d="M80,120C120,120 120,106.66666666666666 160,106.66666666666666"></path>
<path class="link" d="M80,120C120,120 120,133.33333333333334 160,133.33333333333334"></path>
<path class="link" d="M80,40C120,40 120,26.666666666666686 160,26.666666666666686"></path>
<path class="link" d="M80,40C120,40 120,53.333333333333314 160,53.333333333333314"></path>
<path class="link" d="M0,80C40,80 40,40 80,40"></path><path class="link" d="M0,80C40,80 40,120 80,120"></path><g class="node" transform="translate(160,133.3333282470703)">
<circle class="nodeCircle" r="8" style="fill: rgb(255, 255, 255);"></circle><text x="13" dy=".35em" class="hyper" text-anchor="start" style="fill-opacity: 1;">Tiger</text>

In above code, lion comes as plain text but I want to show up one hyperlink.

I have tried to do it with .attr() but it's not working.

You can use replaceWith :

 $(function () { $("text").each(function () { $(this).replaceWith('<a href="">' + $(this).html() + '</a>'); }); }); 
 * {font-family: 'Segoe UI'; margin: 0; padding: 0; list-style: none;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <text x="13" dy=".35em" class="hyper" text-anchor="start" style="fill-opacity: 1;">Lion</text> 

I am not sure what to put for href attribute, but if you need, you can put something yourself, by editing the function.

使用replacewith

$('text').replaceWith('<a href="http://example.com">link</a>')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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