gpt4 book ai didi

jquery - 触发 anchor 链接的点击

转载 作者:行者123 更新时间:2023-12-01 06:14:44 26 4
gpt4 key购买 nike

我有一个链接集合,其中包含与其匹配的缩略图。我需要将这些缩略图加载为 div,并为其设置背景图像。我使用的是包含所有缩略图的单个图像,因此我不能仅将图像加载为图像。单击图像应该与单击链接执行相同的操作,但我制作了一个 JSFiddle 来显示我的意思(并显示问题):

the JSFiddle

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

单击链接将具有额外的功能(统计信息),因此我更喜欢触发对链接的单击,而不是将相同的自定义函数绑定(bind)到 anchor 和 div。

提前致谢

最佳答案

在 HTML4 中,单击事件仅针对输入元素和某些浏览器定义,尤其是 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();

关于jquery - 触发 anchor 链接的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9904170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com