window location href多次跳转导致a标签中的onclick失效

本文探讨了在使用window.location.href进行多次界面跳转时遇到的a标签失效问题,并提供了一个简单有效的解决方案:通过<a href=javascript:history.go(-1);>返回</a>实现上一层的返回。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们通过window.location.href来多次跳转一个界面的时候可能会出现a标签的失效,可以通过

<a  href="javascript:history.go(-1);" >返回</a> 

来进行上一层的返回。

``` <%@ page language="java" import = "java.util.*" import ="javax.swing.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <link rel="stylesheet" href="../style/head_page.css"> <link rel="stylesheet" href="../style/main_function.css"> </head> <body> <div> <input type="button" class = "setting" value = "设置" id = "setting" onclick = "set.show()"> <select name = "gender" class = "account"> <option>一<% /*session.setAttribute("name",1); Window.localStorage.setItem("test", 1); window.localStorage.getItem("name");*/ %></option > <% //查询数据库的账套 %> <option> 二 </option> <option>三</option> <option>保密</option> </select> <input type = "button" id = "exit" class = "exit" value = "退出"> </div> <div id="overlay" style="display:none; position:fixed; top:0; left:0; width:100%; height:100%; background-color:rgba(0,0,0,0.5); z-index:99;"></div> <div class = "main_function"> <input type = "button" value = "查看凭证" onclick = javascript:jump() class = "certificate" > </div> <dialog id = "set" class = "all_set" style = "z-index : 100"> <input type = "button" class = "close" onclick = "set.close()" value = "返回"> <form action = "account_set.jsp"> <input type = "submit" id = "account_set" class = "set1" value = "账套设置>" ></form> <br> <form action = "information.jsp"> <input type = "submit" id = "self_information" class = "set2" value = "个人信息>" ></form> </dialog> <script> const overlay = document.getElementById('overlay'); // 遮罩层 const addAccountDialog = document.getElementById('set'); // 对话框 // 打开弹窗的同时显示遮罩 function showOverlayAndDialog() { overlay.style.display = 'block'; // 显示遮罩 addAccountDialog.showModal(); // 显示弹窗 } // 关闭弹窗的同时隐藏遮罩 function hideOverlayAndDialog() { overlay.style.display = 'none'; // 隐藏遮罩 addAccountDialog.close(); // 关闭弹窗 } // 绑定按钮事件 document.querySelector('.setting').addEventListener('click', showOverlayAndDialog); // 点击“返回”按钮关闭弹窗并隐藏遮罩 document.querySelector('#set input[value="返回"]').addEventListener('click', hideOverlayAndDialog); // 如果用户点击了遮罩区域,则自动关闭弹窗及遮罩 overlay.addEventListener('click', hideOverlayAndDialog); function : jump_certificate() { window.location.href="certificate.jsp"; } </script> </body> </html>```为什么点击查看凭证后不会跳转到相应页面
最新发布
03-16
从你的代码来看,`点击查看凭证` 的功能绑定到了 `onclick` 属性上,并调用了名为 `jump()` 的 JavaScript 函数。然而,在 `<script>` 标签内并没有定义这个函数,而只看到了一个未正确声明的 `function : jump_certificate()`。 以下是详细分析: ### 原因分析 1. **缺少正确的 `jump()` 定义** 按照 HTML 中的配置,按钮的 `onclick` 事件会尝试调用 `javascript:jump()` 函数,但实际上该函数并未正确定义或不存在。 2. **语法错误导致脚本失效** 在 `<script>` 部分中有一段无效的语法:`function : jump_certificate()`,这显然是不合法的 JavaScript 写法。浏览器解析到这部分内容时可能会报错,进而阻止其他脚本正常运行。 3. **拼写或命名混乱** 可能存在命名混淆的情况——开发者原本想引用的是另一个方法(比如 `jump_certificate()`),但由于名称不对或者没有实际赋值给按钮的行为而导致失败。 4. **路径是否有效检查不足** 即便修复上述问题,还需要确认目标文件 `"certificate.jsp"` 是否存在于服务器指定目录下以及其URL地址书写无误;如果路径有问题则同样无法成功导航至新页面。 --- ### 解决方案 #### 修改JS部分以支持正确跳转操作: 你需要修正JavaScript片段并将所有必要的逻辑整合进去。例如: ```javascript <script> const overlay = document.getElementById('overlay'); // 遮罩层 const addAccountDialog = document.getElementById('set'); // 对话框 // ... [保持原有代码不变] /** * 跳转至查看凭证页的功能 */ function jump() { window.location.href = "certificate.jsp"; } /* Optional cleanup of invalid syntax from earlier code block */ </script> ``` 然后确保HTML里的button元素确实指向此修订后的版本: ```html <input type="button" value="查看凭证" onclick="jump()" class="certificate"> ``` #### 进一步验证链接可用性: 最后别忘了测试最终生成的实际URL能否顺利打开预期的目标资源(`certificate.jsp`) 。可以先单独访问一下看看效果如何。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值