[英]How to open an url in new tab using a button in vb.net which using command name not by the click event
[英]How to command+click to open url in new tab and switch to it in JavaScript?
有一個div
元素。 目標是檢測命令+單擊以在新選項卡中打開一些 URL並切換到新選項卡。 我已經做了檢測並打開了。
問題是:我用同樣的方法打開 URL :只需使用window.open()
。 當我按下命令並單擊時,它不會切換到新選項卡。 當我單擊時,它會切換。
<div
onClick={(event) => {
if (event.metaKey || event.ctrlKey) {
// will not switch, but why?
openLink('https://stackoverflow.com/');
}
}}
>
command Click or ctrl Click
</div>
<div
onClick={(event) => {
// will switch
openLink('https://stackoverflow.com/');
}}
>
just click
</div>
所以我想知道如何讓命令+單擊打開一個新選項卡並切換到它? 謝謝你的幫助。
這是CodeSandbox上的最小代碼片段。
問題解決了。 我將setTimeout
添加到它,它會切換。 但是,我仍然不知道為什么......
<div onClick={(event) => {
if (event.metaKey || event.ctrlKey) {
// it works, but why?
setTimeout(() => openLink("https://stackoverflow.com/"));
}
}}>
command Click or ctrl Click
</div>
嘗試使用event.ctrlKey
,它將返回一個 boolean 指示是否按下 ctrl
<div onClick={(event) => {
if (event.metaKey || event.ctrlKey) {
// will not switch, but why?
openLink("https://stackoverflow.com/");
}
}}
>
command Click or ctrl Click
</div>
這將有效,但對於簡單的單擊和 cmd+單擊。
<div onClick={()=> window.open("someLink", "_blank")}>text</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.