简体   繁体   English

为什么当onClick链接起作用时,href链接不能起作用?

[英]Why doesn't a href link work while an onClick link does?

In my jquery slider here, I am trying to wrap the images in tags with href links. 在这里的jquery滑块中,我试图将图像包装在带有href链接的标签中。 They are unresponsive. 他们没有反应。 However, onclick="javascript:self.location.href='http://hodaradesign.com/'; return false;" 但是,onclick =“ javascript:self.location.href ='http://hodaradesign.com/';返回false;” works fine. 工作正常。 Why is that? 这是为什么?

Doesn't work: 不起作用:

<a href="hodaradesign.com"><img id="book2" class="book" src="book2.png" /></a>

Works: 作品:

<img id="book3" class="book" src="book3.png" onclick="javascript:self.location.href='http://hodaradesign.com/'; return false;" />

Live demo: 现场演示:

http://www.freewaycreative.com/jsfun/fiddle.html http://www.freewaycreative.com/jsfun/fiddle.html

Any ideas? 有任何想法吗?

well this looks somewhat like spaghetti code. 好吧,这看起来有点像意大利面条代码。

you dont need the javascript: handler in an onclick event as its already a javascript event. 您不需要onclick事件中的javascript:处理程序,因为它已经是javascript事件。 furthermore the return false in the onclick event will prevent the link from beeing followed if javascript is enabled and executed correctly. 此外,如果启用并正确执行了javascript,则onclick事件中的return false将阻止链接被跟踪。

something like this or some other listener is probably your problem. 像这样的声音或其他监听器可能是您的问题。

you specified the href attribute on the <img> tag instead of the <a> tag. 您在<img>标记而不是<a>标记上指定了href属性。

it should be: 它应该是:

<a href="http://hodaradesign.com"><img id="book2" class="book" src="book2.png" /></a>

if you return false in a onlckick event handler, the event es bing prevented from being executed. 如果在onlckick事件处理程序中返回false,则将阻止执行该事件。 in this case opening the link. 在这种情况下,打开链接。

Because your slider doesn't know or care about the <a...> element, it's only looking at elements with the class set to book ! 由于您的滑块不了解或不在意<a...> ...>元素,因此仅查看设置为book元素!

Try this: 尝试这个:

<a class="book" href="http://hodaradesign.com">
  <img id="book2" src="book2.png" />
</a>

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

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