简体   繁体   中英

onclick=“location.href='link.html'” is not working

trying to figure out how to add onclick="location.href='link.html'". so that when the user clicks on image it will take the user to a url. Just not sure how to include this in the code below. Here is my code...

<a class="thumbnail even" 
   onClick="_gaq.push(['_trackEvent', 'holiday', 'logo', 'jcpenney']);" 
   onmouseover="changeImgSrc('JCP_FullImage_321x400.png')"
   onmouseout="document.getElementById('partnerHoverImg').src='../images/FPO_FullImage_321x400.png'">
   <img src="../images/JCPenney_logo_150x110.png"/></a>

Been looking at this for a minute and any help would be greatly appreciated! Thanks!

You already have it wrapped in a link. Just set the href:

<a href="PUT YOUR LINK HERE" class="thumbnail even" onClick="_gaq.push(['_trackEvent', 'holiday', 'logo', 'jcpenney']);" onmouseover="changeImgSrc('JCP_FullImage_321x400.png')" onmouseout="document.getElementById('partnerHoverImg').src='../images/FPO_FullImage_321x400.png'"><img src="../images/JCPenney_logo_150x110.png"/></a>

Please separate your UI from your functionality. It iwll make your code a lot more readable and easier to maintain. That said, you could stick the link in a data attribute inside the img - something like this:

<img class="RedirectImage" data-url="www.google.com" src="blah.jpg"/>

JQuery:

$(".RedirectImage").click(function(e){
    document.location.href = $(this).data("url");
});

You should just add an href="link.html" to the a tag as others have suggested.

If you can't do that and you can't add another handler with JavaScript, you just need to add another statement within the onclick attribute

<a class="thumbnail even" 
   onclick="_gaq.push(['_trackEvent', 'holiday', 'logo', 'jcpenney']);
            location.href='link.html'" 
   onmouseover="changeImgSrc('JCP_FullImage_321x400.png')"
   onmouseout="document.getElementById('partnerHoverImg').src = 
               '../images/FPO_FullImage_321x400.png'" 
>
   <img src="../images/JCPenney_logo_150x110.png"/>
</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