当前位置:首页 > JavaScript

js实现点击跳转

2026-03-01 04:53:39JavaScript

使用 window.location.href 实现跳转

通过修改 window.location.href 可以直接跳转到目标 URL:

document.getElementById('button').addEventListener('click', function() {
  window.location.href = 'https://example.com';
});

使用 window.open() 实现新标签页跳转

如果需要在新标签页打开链接,可以使用 window.open()

js实现点击跳转

document.getElementById('button').addEventListener('click', function() {
  window.open('https://example.com', '_blank');
});

使用 <a> 标签模拟点击

动态创建或触发 <a> 标签的点击事件:

js实现点击跳转

const link = document.createElement('a');
link.href = 'https://example.com';
link.target = '_blank'; // 可选:新标签页打开
link.click();

使用 location.assign() 跳转

location.assign() 会记录跳转历史,支持后退操作:

document.getElementById('button').addEventListener('click', function() {
  location.assign('https://example.com');
});

使用 location.replace() 跳转(无历史记录)

直接替换当前页面,不保留历史记录:

document.getElementById('button').addEventListener('click', function() {
  location.replace('https://example.com');
});

注意事项

  • 跳转前可添加条件判断或异步操作(如确认弹窗)。
  • 部分浏览器可能拦截 window.open() 的弹出行为,需用户主动触发事件。

标签: 跳转js
分享给朋友:

相关文章

vue文件实现页面跳转

vue文件实现页面跳转

使用 router-link 实现跳转 在 Vue 模板中直接使用 <router-link> 组件,通过 to 属性指定目标路径: <router-link to="/tar…

js实现计算器

js实现计算器

实现基础计算器功能 使用JavaScript创建一个基础计算器需要处理数字输入、运算符逻辑和结果显示。以下是一个简单的实现示例: let currentInput = '0'; let previo…

vue 实现跳转

vue 实现跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,以下是常见的几种方法: 使用 router-link 组件 router-link 是 Vue Router 提供的组件,用于…

js分组实现

js分组实现

分组实现方法 在JavaScript中,可以通过多种方式实现数组或对象的分组操作。以下是几种常见的方法: 使用Array.prototype.reduce() 通过reduce方法可以轻松实现数组分…

js怎么实现异步

js怎么实现异步

异步实现方法 JavaScript中实现异步操作主要通过以下几种方式: Promise Promise是ES6引入的异步解决方案,用于处理异步操作的成功或失败状态。 const promise =…

js实现图片

js实现图片

图片加载与显示 在JavaScript中,可以通过Image对象动态加载图片。创建实例后设置src属性触发加载,通过onload回调处理加载完成后的操作: const img = new Ima…