触发对锚链接的点击 [英] trigger a click on a anchor link
问题描述
我有一组链接,缩略图与它们匹配。我需要加载这些缩略图作为div的背景图像设置为他们。我使用的图片中包含所有缩略图,因此我无法将图片加载为图片。点击图片应该像点击链接一样,我做了一个JSFiddle显示我的意思(并显示问题):
I have a collection of links with thumbnails that match them. I need to load these thumbnails as divs with the background image set to them. I'm using a single image that has all thumbnails in them, so I can't just load the images as images. Clicking the image should do the same as clicking the link though, I made a JSFiddle to show what I mean (and to show the problem):
当我点击两个链接时,一个新窗口打开有我想要的网站。但是当我点击缩略图,我不能得到点击链接触发。
When I click the two links in there a new window is opened which has the site I want. But when I click the thumbnails I can't get a click on the link triggered.
点击链接将有额外的功能(统计),所以我喜欢通过绑定相同的自定义函数到锚点和div 。
Clicking of the link will have extra functionality (stats), so I prefer to trigger a click on the link over binding the same custom function to both the anchors and the divs.
提前感谢
推荐答案
Hiya : http://jsfiddle.net/f6FJ3/19/
触发原因请阅读: jQuery:trigger click()无效?
重要的是要澄清jQuery('#open')。click()不会执行href属性锚定标记,因此您不会被重定向。它执行未定义的#open的onclick事件。
以下代码将获得窗口单击时打开的窗口的输出。
below code will get the desired output of window getting open on image click.
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'));
});
});
希望这有助于,欢呼!
这篇关于触发对锚链接的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!