javascript - Link not working on image? -
i've been attempting create effect user clicks on image, image replaced image acts link.
however problem whenever click replaced image, link doesn't work.
fiddle: http://jsfiddle.net/ha6qp7w4/321/
$('.btnclick').on('click',function(){ $(this).attr('src','https://placekitten.com/g/200/300') $(this).attr('href','google.com') });
img
tags don't have href
properties. need wrap image in anchor , assign url that, or custom redirect.
notice image html on inspection of element:
<img src="https://placekitten.com/g/200/300" id="1" class="btnclick" href="google.com"> <!-- not valid! -->
this isn't valid because imgs aren't anchors!
function first() { this.src = 'https://placekitten.com/g/200/300'; $(this).unbind("click"); $(this).on("click", second); } function second() { window.location.href = "https://www.google.com"; $(this).unbind("click"); $(this).on("click", first); } $('.btnclick').on('click', first);
(i tried make fiddle wouldn't save, should work)
you need store actions in functions can revert if need be. first action change source, change event redirect link.
Comments
Post a Comment