Reputation: 39
I am trying to add target="_blank"
to my SVG <a/>
tag but I can't figure out why my code isn't working.
<script src="http://code.jquery.com/jquery-2.2.3.min.js"></script>
<script>
function loadedSVG() {
$('#svg1 a').attr('target', '_blank');
}
</script>
<object id="svg1" data="SVG_File.svg" type="image/svg+xml" onload='loadedSVG()'>
<a href="http://www.yahoo.com/">yahoo</a>
</object>
The code works on the Yahoo link but not on hrefs within the SVG file, anybody have any idee how I can fix this?
Upvotes: 1
Views: 130
Reputation: 1033
You should put object
tag in a
tag
<script src="http://code.jquery.com/jquery-2.2.3.min.js"></script>
<script>
function loadedSVG() {
$('#svg1 a').attr('target', '_blank');
}
</script>
<a href="http://www.yahoo.com/">
<object id="svg1" data="SVG_File.svg" type="image/svg+xml" onload='loadedSVG()'>
yahoo
</object>
</a>
hope that's help
Upvotes: 0
Reputation: 51
I think that the link must be outer of object tag
function loadedSVG() {
$('#container a').attr('target', '_blank');
}
#container{
position: relative;
width: 150px;
height: 150px;
}
a{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: blue;
color: white;
}
#svg1{
width: 100%;
height: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div id="container">
<a href="http://www.yahoo.com/">yahoo</a>
<object id="svg1" data="https://la-cascade.io/content/images/2015/06/kiwi.svg" type="image/svg+xml" onload='loadedSVG()'>
</object>
</div>
Regards
Upvotes: 1