简体   繁体   English

使用javascript或jquery模拟ctrl +单击(打开没有焦点的新选项卡)

[英]simulate ctrl + click with javascript or jquery (to open a new tab without focus)

I am playing with the jquery event object but I am stuck as hell 我正在玩jquery事件对象,但我被困在地狱

I read the API https://api.jquery.com/category/events/event-object/ but it's not really helping here, I am not even sure it's a good lead to do that 我阅读了API https://api.jquery.com/category/events/event-object/但它在这里并没有真正起作用,我甚至不确定这是一个很好的领导

Do you have any suggestion ( the problem is to do the exact ctrl + click on a link). 你有什么建议(问题是要完全按住ctrl +点击一个链接)。 I saw some posts about it but nothing seems to work on the recent browsers 我看到了一些关于它的帖子,但似乎在最近的浏览器上没有任何效果

very simple exemple : 非常简单的例子:

<span id="toto">toto</span>
<a href="https://google.fr" id="inANewTab"></a>

// The goal is when I click on #toto, I would like #inANewTab trigger 
// in a new tab without focus. To do that I was thinking
// about replicate a ctrl+click event

$('#toto').click(function(){
  ???
})

Edit: 编辑:

The Event object in jQuery has a parameter for ctrlKey, you could assign that as true, on click. jQuery中的Event对象有一个ctrlKey参数,你可以在点击时将其指定为true。

var e = jQuery.Event("click");
e.ctrlKey = true;
$('#id').trigger(e);

Reference: jquery trigger ctrl + click 参考: jquery触发器ctrl +单击

This is a non-jQuery version to simulate keyboard events. 这是一个非jQuery版本来模拟键盘事件。 This works in both Chrome (WebKit based) and Firefox (Gecko based): 这适用于Chrome(基于WebKit)和Firefox(基于Gecko):

var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";


keyboardEvent[initMethod](
                   "keydown", // event type : keydown, keyup, keypress
                    true, // bubbles
                    true, // cancelable
                    window, // viewArg: should be window
                    false, // ctrlKeyArg
                    false, // altKeyArg
                    false, // shiftKeyArg
                    false, // metaKeyArg
                    40, // keyCodeArg : unsigned long the virtual key code, else 0
                    0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
document.dispatchEvent(keyboardEvent);

Or using jQuery, you can simulate by jQuery's event object: 或者使用jQuery,您可以通过jQuery的event对象进行模拟:

jQuery.event.trigger({
  type:  'keypress',
  which: character.charCodeAt(0)
});

In pure javascript, you can use the MouseEvent for that: 在纯JavaScript中,您可以使用MouseEvent

document.getElementById("todo").dispatchEvent(new MouseEvent("click", {ctrlKey: true}));

To programatically open a new tab you can do that: 要以编程方式打开新选项卡,您可以执行以下操作:

const a = document.createElement("a");
a.setAttribute("href", "https://www.google.com/");
a.dispatchEvent(new MouseEvent("click", {ctrlKey: true}));

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

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