.click() 和实际单击按钮之间的区别?(javascript/jQuery) [英] Difference between .click() and actually clicking a button? (javascript/jQuery)

查看:24
本文介绍了.click() 和实际单击按钮之间的区别?(javascript/jQuery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出我一直遇到的这个奇怪的问题,根本原因是实时点击和触发 .click() 之间的差异.

I'm trying to figure out this weird issue I've been having, and the root cause is the difference between a live click and triggering .click().

我不会深入探讨问题的细节,但基本上当你点击输入按钮时它工作正常(有一个 onclick 事件).但是如果我从其他地方调用 .click() (而不是物理点击按钮),它就不能正常工作.

I won't get into the details of the problem, but basically when you click on the input button it works fine (has an onclick event). But if I call .click() from somewhere else (instead of physically clicking the button) it doesn't work properly.

我的问题是 - 有没有办法真正复制对按钮的实际点击?

My question is - is there a way to truly replicate an actual click on a button?

编辑

问题:我正在打开一个加载内嵌 PDF 的新窗口(aspx 页面).如果我真的点击了链接,窗口会正常打开并加载 PDF.如果我使用 .click() 窗口打开并提示我下载 PDF 文件. 我已经通过 Adob​​e Reader 设置、浏览器设置和注册表设置有关提示- 我知道这些可能是大局的因素,但现在我担心为什么鼠标点击和 .click() 之间的行为完全不同.

The problem: I'm opening a new window (aspx page) that loads an inline PDF. If I actually click on the link, the window opens fine and the PDF loads. If I use .click() the window opens and I'm prompted to download the PDF file. I've been through Adobe Reader settings, browser settings, and registry settings about the prompting - I understand that these can be factors to the big picture, but right now I'm concerned about why the behavior between a mouse click and .click() are doing anything different at all.

推荐答案

我使用这个功能来真正模拟鼠标点击:

I use this function to truly mimic a mouse click:

function clickLink(link) {
    var cancelled = false;

    if (document.createEvent) {
        var event = document.createEvent("MouseEvents");
        event.initMouseEvent("click", true, true, window,
            0, 0, 0, 0, 0,
            false, false, false, false,
            0, null);
        cancelled = !link.dispatchEvent(event);
    }
    else if (link.fireEvent) {
        cancelled = !link.fireEvent("onclick");
    }
}

这篇关于.click() 和实际单击按钮之间的区别?(javascript/jQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆