<a>的href中添加js函数的写法

本文介绍了如何使用JavaScript实现页面刷新功能,并探讨了void(0)在HTML超链接中的使用方式及注意事项。

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

参考资料:
http://www.cnblogs.com/opper/archive/2009/01/12/1373971.html

1 void(0) 表示一个死链接
可以在void中执行js脚本。但是不能用;结尾。
例如


<a class="btn btn-default" href="javascript:void(window.location.reload();)" role="button">Cancel</a>


以上刷新页面的脚本不会执行。但是若去掉;就会执行。

2 建议将js放在void之前的写法。
例如

<a class="btn btn-default" href="javascript:window.location.reload();void(0)" role="button">Cancel</a>


这样的写法就必须加上“;”结束符号。若不加,会有语法错误哦。报错信息:
SyntaxError: missing ; before statement


window.location.reload() void(0)
{% extends "base.html" %} {% block title %}系统管理{% endblock %} {% block content %} <div class="admin-panel"> <h1>管理控制台</h1> <!-- 权限验证 --> {% if not current_user.is_admin %} <div class="alert alert-danger"> 错误:您没有访问此页面的权限 </div> {% else %} <section class="admin-section"> <h2>用户管理</h2> <table class="data-table"> <thead> <tr> <th>ID</th> <th>用户名</th> <th>最后登录</th> </tr> </thead> <tbody> {% for user in user_list %} <tr> <td>{{ user.id }}</td> <td>{{ user.username }}</td> <td>{{ user.last_login | datetimeformat }}</td> </tr> {% endfor %} </tbody> </table> </section> {% endif %} </div> {% endblock %}{% extends "base.html" %} {% block title %}用户登录{% endblock %} {% block content %} <form class="auth-form" method="POST" action="{{ url_for('login') }}"> <h2>用户登录</h2> <!-- CSRF保护 --> <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"> <div class="form-group"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required pattern="[A-Za-z0-9]{3,20}" title="仅允许字母和数字 (3-20字符)"> </div> <div class="form-group"> <label for="password">密码:</label> <input type="password" id="password" name="password" required minlength="8" title="至少8位字符"> </div> <button type="submit" class="btn-primary">登录</button> <!-- 安全增强措施 --> <div class="security-tips"> <p>🔒 采用HTTPS加密传输</p > <p>⚠ 登录失败3次将锁定账户</p > </div> </form> {% endblock %}<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{% block title %}安全增强网站{% endblock %}</title> <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> </head> <body> <header class="navbar"> <nav> 首页 {% if current_user.is_authenticated %} 登出 {% if current_user.is_admin %} 管理后台 {% endif %} {% else %} 登录 {% endif %} </nav> </header> <main class="container"> <!-- 安全警告显示区域 --> {% with messages = get_flashed_messages(with_categories=true) %} {% if messages %} {% for category, message in messages %} <div class="alert alert-{{ category }}"> {{ message }} </div> {% endfor %} {% endif %} {% endwith %} {% block content %}{% endblock %} </main> <script src="{{ url_for('static', filename='main.js') }}"></script> </body> </html>这三个代码和你写的相比真么样
03-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值