I'm facing a challenge with importing SVG code embedded in an HTML file and using the v-html directive to render it. The issue is that I need to attach click events to the anchor tags within that imported file, which are not part of my main template.
Is there a way to retrieve all the anchor tags from the imported file?
And how can I bind events to these elements without relying on the v-on directive?
The template structure:
<v-flex mt-2 px-0 xs6
ref="map_svg"
v-html="mapHTML">
</v-flex>
Excerpt from the imported file:
<svg>
<g>
<g>
<a target="_blank" xlink:href="url to a subsite" >
<!-- content path which shows a circle -->
</a>
</g>
<g>
<a target="_blank" xlink:href="url to a differnt subsite" >
<!-- content path which shows a circle -->
</a>
</g>
<!-- additional similar elements as shown above -->
</g>
</svg>
I want to add a click event to these anchor tags and either remove or change the xlink:href attribute so that they don't open new tabs in the browser upon clicking.
Edit
This was the solution I implemented:
mounted() {
const that = this;
const links = this.$el.querySelectorAll('#map_svg svg > g > g > a');
links.forEach(el => {
const url = el.getAttribute('xlink:href');
if (url) {
// el.addEventListener('click', that.stationClick(url));
el.onclick = function (){
that.stationClick(url);
}
el.removeAttribute('xlink:href');
}
el.removeAttribute('target');
});
},