如何从下拉列表中打开新选项卡中的链接



我正在尝试使用下面的javascript函数在新选项卡中打开下拉列表中的项目。截至目前,将显示一个下拉列表,每个项目将仅在同一窗口中打开。

任何帮助都将不胜感激!

HTML

<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Button Name</button>
<div id="myDropdown" class="dropdown-content">
<a href="">Choice A</a>
<a href="">Choice B/a>
</div>
</div>

JS

function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}

您根本不需要JavaScript。只需在<a>标签中将target属性设置为_blank,如下所示:

function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
.show{
display:block !important;
}
#myDropdown{
display:none;
}
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Button Name</button>
<div id="myDropdown" class="dropdown-content">
<a href="https://choice.a" target="_blank">Choice A</a>
<a href="https://choice.b" target="_blank">Choice B</a>
</div>
</div>

将值为_blanktarget属性添加到a标记中。

像这样:

<a href="https://google.com" target="_blank">Google</a>

最新更新