2012-03-28 160 views
1

我有一个链接与缩略图相匹配的集合。我需要将这些缩略图加载为背景图像设置为div的div。我使用的是单个图像,其中包含所有缩略图,因此我不能将图像作为图像加载。点击图片应该做同样的点击链接不过,我做了一个的jsfiddle来说明我的意思(并显示问题):触发一个锚链接点击

the JSFiddle

当我点击有一个新的窗口,两个环节打开它有我想要的网站。但是当我点击缩略图时,我无法点击所触发的链接。

点击链接将有额外的功能(统计数据),所以我更喜欢触发链接的点击过相同的自定义功能结合到锚和的div两者。

在此先感谢

回答

5

你好工作演示在这里:http://jsfiddle.net/f6FJ3/19/

的触发原因阅读:jQuery: trigger click() doesn't work?

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')); 

    }); 
});​ 

希望这有助于,干杯!

7

在HTML4 click事件被定义为仅输入元素和一些浏览器中,最值得注意的是FF,strictly followed the specification。然而HTML5游戏的改变,任何现代的浏览器实现了这个功能, jQuery的仍然有对于这个事件的特殊情况(摘自jquery trigger method):

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();