简体   繁体   中英

svg or javascript to replace url part of use

I have some:

<svg class="image image-size-09em" role="img" area-hidden="true">
  <use href="_content/blazor/blazor.svg#chevron-right"></use>
</svg>

I want to replace chevron-right with chevron-left for all svgs in the page. How is it possible?

Use querySelectorAll to select all the relevant elements, then change all of their hrefs

 document.querySelectorAll("use").forEach(element=>{ element.href.baseVal = element.href.baseVal.replace("chevron-right", "chevron-left") })
 <svg class="image image-size-09em" role="img" area-hidden="true"> <use href="_content/blazor/blazor.svg#chevron-right"></use> </svg>

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