有关jsp中onclick事件无效的原因,以及jsp中页面加载顺序的问题

本文探讨了一种常见的JSP页面中onclick事件无法触发的情况,并详细解释了问题产生的原因及解决方案。通过调整函数定义的位置,使事件能够正常响应。

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

今天在写jsp页面时遇到一个有关onclick事件无效的问题,在这里分享一下:

   <body>
  <div>
   <input type="text" id="name"/>
  </div>
  <div id="hint">
   <ul id="ul" >
    <li onclick="voluation();">aa</li>
   </ul>
  </div>
  </body>
     <script type="text/javascript">
    $(function()
    {
        function voluation()
        {
            alert("方法被调用");
        }
    });  
  </script>

如上代码发现绑定的onclick事件怎么都不能被触发,最后发现voluation()函数写在(function())(function())是在页面加载完的情况下才会执行里面的方法,也就是说在onclick事件上绑定的这个函数是不存在的。

只需要把voluation()函数定义到外面就可以了

  <body>
  <div>
   <input type="text" id="name"/>
  </div>
  <div id="hint">
   <ul id="ul" >
    <li onclick="voluation();">aa</li>
   </ul>
  </div>
  </body>
     <script type="text/javascript">
    $(function()
    {

    });  
    function voluation()
    {
        alert("方法被调用");
    }
  </script>

那么问题又来了jsp是从上至下执行,为什么voluation()函数在绑定事件的下面也会被绑定上呢?这是因为页面加载过程中,浏览器会对页面上或载入的每个js代码块(或文件)进行扫描,如果遇到定义式函数,则进行预处理,处理完成之后再开始由上至下执行;遇到赋值式函数,则只是将函数赋给一个变量,不进行预处理,待调用到的时候才进行处理。

``` <%@ 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值