触发锚定链接上的单击

我有一系列链接,其中包含与之匹配的缩略图。 我需要将这些缩略图加载为div,并将背景图像设置为div。 我正在使用其中包含所有缩略图的单个图像,因此我无法将图像作为图像加载。 单击图像应该像点击链接一样,我做了一个JSFiddle来显示我的意思(并显示问题):

JSFiddle

当我点击那里的两个链接时,会打开一个新窗口,其中包含我想要的网站。 但是当我点击缩略图时,我无法点击触发的链接。

单击该链接将具有额外的function(统计数据),因此我更喜欢通过将相同的自定义函数绑定到锚点和div来触发链接上的单击。

提前致谢

Hiya 工作演示: http //jsfiddle.net/f6FJ3/19/

对于触发原因,请阅读: jQuery:触发器click()不起作用?

It's important to clarify that doing jQuery('#open').click() does not execute the href attribute of an anchor tag so you will not be redirected. It executes the onclick event for #open which is not defined.

下面的代码将在图像点击时获得所需的窗口输出。

JQuery代码

 $(document).ready(function(){ $('.tmb').click(function(){ var target=$(this).parent().find("a"); $("#debug").text("clicked "+target.text()); //target.triggerHandler('click'); window.open(target.attr('href')); }); });​ 

希望这会有所帮助,欢呼!

在HTML4中,click事件仅为输入元素定义,而某些浏览器(尤其是FF) 严格遵循规范 。 然而HTML5游戏改变了,任何现代浏览器都实现了这个function, 但是 jQuery仍然有一个特殊情况(从jquery触发器方法中提取):

 if ( !onlyHandlers && !event.isDefaultPrevented() ) { if ( (!special._default || special._default.apply( elem.ownerDocument, data ) === false) && !(type === "click" && jQuery.nodeName( elem, "a" )) && jQuery.acceptData( elem ) ) { // Call a native DOM method on the target with the same name name as the event. 

作为解决方法,您可以从以下位置更改代码:

 target.trigger("click"); 

 target.get(0).click();