简体   繁体   English

onclick =“ location.href ='link.html'”不起作用

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

trying to figure out how to add onclick="location.href='link.html'". 试图弄清楚如何添加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: 只需设置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内的data属性中,如下所示:

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

JQuery: 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. 您应该像其他人建议的那样,在标签中添加href="link.html"

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 如果您无法执行此操作,并且无法使用JavaScript添加其他处理程序,则只需在onclick属性中添加另一条语句

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM