简体   繁体   English

如何使用javascript模拟点击锚标记?

[英]How to emulate click on anchor tag with javascript?

On my site, I have a div container, which contains a <input> , and a <a onclick="DoSomeStuff();" href="#" target="_blank"> 在我的网站上,我有一个div容器,其中包含<input><a onclick="DoSomeStuff();" href="#" target="_blank"> <a onclick="DoSomeStuff();" href="#" target="_blank"> element, which acts as a button. <a onclick="DoSomeStuff();" href="#" target="_blank">元素,用作按钮。 Now, DoSomeStuff(); 现在, DoSomeStuff(); modifies the href attribute based on the value of the input element. 根据input元素的值修改href属性。

I need to emulate click on this anchor tag after user pressed enter on an input element. 用户在input元素上按Enter键后,我需要模拟单击此锚标记。 I know how to detect this enter, but I don't know how to click this anchor. 我知道如何检测此输入,但我不知道如何单击此锚点。 jQuery's .click() is not working in here, it only fires onclick() of the anchor tag, but does not actually click the anchor. jQuery的.click()在这里不起作用,它只触发锚标记的onclick() ,但实际上并没有点击锚点。

Edit : Here is an example on what I want to do: http://jsfiddle.net/JZYWZ/ . 编辑 :以下是我想要做的一个例子: http//jsfiddle.net/JZYWZ/ It triggers the onclick event of the anchor tag, but does not follow the link in new tab. 它会触发锚标记的onclick事件,但不会跟随新标签中的链接。

Browsers are very strict about opening links in new tabs/windows through javascript. 浏览器对通过javascript打开新标签/窗口中的链接非常严格。

If they allowed you to have javascript click and open a new tab when the user presses enter (for what you know to be legitimate reasons), that feature could be abused by spammers to have javascript click ads and spam links when the user does anything . 如果他们允许您点击javascript并在用户按下enter时打开一个新标签(因为您知道这是合法的原因),垃圾邮件发送者可能滥用该功能,以便在用户执行任何操作时使用javascript点击广告和垃圾链接。 Sadly there's no way the browser can tell your linked page is legitimate and not spam or monetised clickbait. 遗憾的是,浏览器无法判断您的链接页面是否合法,而不是垃圾邮件或货币化的clickbait。

Pretty much the only option to open the url in new tab using javascript is window.open which you've already tried: the browser will always look to the user's settings on popup blocking and on whether to open in tabs or windows - and the default settings are strict, particularly in Firefox and Internet Explorer (less so in Chrome and Safari). 几乎是使用javascript在新标签页面中打开网址的唯一选项是你已经尝试过的window.open :浏览器将始终在弹出窗口阻止以及是否在标签页或窗口中打开时查看用户的设置 - 默认情况下设置是严格的,特别是在Firefox和Internet Explorer中(在Chrome和Safari中较少)。


A possible alternate approach where you won't be fighting the browser's anti-spam defences is to open the content in a modal overlay (iframe-based if you need multiple pages). 您可能不会对浏览器的反垃圾邮件防御措施进行打击的另一种方法是在模式覆盖中打开内容(如果您需要多个页面,则基于iframe)。 Here's one example library . 这是一个示例库

It'll be similar from a UI point of view - giving the user related content in a form where they can just close it and return to the main page when they're done with it. 从UI的角度来看,它将是类似的 - 以一种形式向用户提供相关内容,他们可以将其关闭并在完成后返回主页面。

Modal overlays are widely used and likely to be familiar with users. 模态叠加被广泛使用并且可能熟悉用户。 For example, they're now pretty standard for sharing pages, logins and other pieces of simple interaction or side content. 例如,它们现在非常适合共享页面,登录和其他简单交互或侧面内容。 Example : 示例

在此输入图像描述

EDIT 编辑

After some additional testing.... 经过一些额外的测试....

$('input[type="text"]').on('keyup', function(event){
    if(event.keyCode === 13){
        DoSomeStuff();
        window.open($('a').attr('href'));      
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM