我希望下面的按钮代码打开一个新选项卡,其中包含从 js 变量中获取的 URL

I want the below button code to open a new tab with the URLfetched from a js variable

你能帮我解决以下问题吗? 我有一个按钮代码如下所示:

    <button type="button" onclick= "location.href=document.getElementById('Text1').value;return false;" class="btn btn-danger">Launch Console</button>

这里我从 javascript 变量中获取了 URL 值。无论如何我可以在新标签页中打开相同的内容吗?目前它正在同一 window/tab.

中加载

你可以这样做:

function openInNewTab(url) {
  var win = window.open(url, '_blank');
  win.focus();
}

<button type="button" onclick= "openInNewTab(urlHere)" class="btn btn-danger">Launch Console</button>
<input id=text type=text>
<button onClick="window.open(document.getElementById('text').value, '_blank');">URL In New Tab</button>

就像我说的,仔细阅读副本。并且不要使用 location.href.

DEMO