javascript - 如何: On click open new page in new tab and redirect the old one

标签 javascript html

一直在尝试找到一些解决方案,但我无法找到。我需要一个按钮来一次点击触发两个事件。此代码适用于 Firefox,但不适用于 Chrome:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>EXAMPLE</title>
  <script type="text/javascript">
      function changeLocation (){
  window.location = "https://www.bing.com";
      }
  </script>
</head>
<body>
<form action="https://www.google.com" target="_blank">
  <input type="text">
  <button onclick="changeLocation()">Submit</button>
</form>
</body>
</html>

我正在尝试将用户重定向到新选项卡中的新页面,并在单击按钮后重定向主页(旧页面)。 是否有任何解决方案也适用于 Chrome?

非常感谢任何帮助!

最佳答案

function changeLocation() {
document.getElementById("form").submit();
window.open("https://www.bing.com","_blank");
}
<form id="form" action="https://www.google.com">
<input type="text">
<button onclick="changeLocation()">Submit</button>
</form>

这对你有帮助。顺便说一句,您需要使用输入标签中的名称属性来提交数据。

关于javascript - 如何: On click open new page in new tab and redirect the old one,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48973131/

相关文章:

javascript - 关于PHP、MYSQL、AJAX的问题

javascript - 获取移动页面的比例

javascript - 加速 Gatsby 性能

javascript - 滚动顶部时发现奇怪的行为

html - X3D计算IndexedFaceSet coordIndex

html - HTML 表单(GET)中使用的方法与 HTTP GET 不同吗?

php - 在一个引导轮播中显示多个图像时出错

javascript - 使用带 Angular js 的灰尘模板

html - 如何创建一个跨越整个 div 宽度的字母选择器

html - 网页的字体大小和行高