<a>标签的href和onclick

本文探讨了HTML中&lt;a&gt;标签的href属性和onclick事件导致的搜索按钮被点击两次的问题,并提供了几种有效解决方案。

这是前两天的代码刚刚遇到的一个问题:

一个普通的条件查询:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <form>  
  2.     用户名:<input type="text" id="userName">  
  3.     登录名:<input type="text" id="loginName">  
  4.     <a href="" onclick="search()">搜索</a>  
  5. </form>  


接下来的问题就是:

search()中提交的url,总是会被执行两次。

问题的根源:

<a>标签的href和onclick

详解:

 如果同时存在href和onclick,首先执行onclick事件,其次是href属性下的动作。

 如果想让href属性下的动作不执行,那onclick必须得到一个false的返回值,或者将href的属性设置为无效链接。即:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <a href="#" onclick="search();return false;">搜索</a>  
  2. <a href="javascript:void(0)" onclick="search()">搜索</a>  
  3. <a href="#" onclick="search();event.returnValue=false">搜索</a>  

#和javascript:void(0)是常见的两个href属性,其区别是javascript:void(0)没起实质上的作用,仅仅是一个死链接,执行的是onclick的事件,"#"包含了一个位置信息默认的锚#top,也就是网页的上端。如果页面过长,有滚动条,且希望通过onclick事件执行操作,应将它的href属性设置为javascript:void(0),而不是#,以防止不必要的页面跳动。

增加css样式 <?php $content = $_GET['content']; echo "<h2>后台管理 - $content</h2><p>这是 $content 的具体操作内容。</p>"; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="styles.css"> <title>后台管理布局</title> <script> function showContent(fileName, content) { const mainContent = document.querySelector('.main-content'); const xhr = new XMLHttpRequest(); xhr.open('GET', fileName + '?content=' + content, true); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { mainContent.innerHTML = xhr.responseText; } }; xhr.send(); } </script> </head> <body> <div class="sidebar"> <ul> <li><a href="#" onclick="showContent('lm.php', '功能1')">添加栏目</a></li> <li><a href="#" onclick="showContent('lmlb.php', '功能2')">栏目列表</a></li> <li><a href="#" onclick="showContent('index.php', '功能3')">首页</a></li> <li><a href="#" onclick="showContent('xdr.php', '功能3')">用户注册</a></li> <li><a href="#" onclick="showContent('yonghu.php', '功能3')">用户列表</a></li> <li><a href="#" onclick="showContent('xinwen/zj.php', '功能3')">新闻列表管理</a></li> <li><a href="#" onclick="showContent('xinwen/category_list.php', '功能3')">新闻分类管理</a></li> <li><a href="#" onclick="showContent('xinwen/review.php', '功能3')">审核新闻</a></li> </ul> </div> <div class="main-content"> <h2>操作区域</h2> <p>这里显示具体的操作内容。</p> <p>点击上面需要查看的内容。</p> </div> </body> </html>
最新发布
12-11
### 网上点餐系统中菜品查询操作的HTML结构交互实现流程 #### HTML结构设计 在设计网上点餐系统的菜品查询页面时,可以使用HTML5中的多种语义化标签来构建清晰的结构。以下是一个典型的HTML结构示例: ```html <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>菜品查询</title> </head> <body> <!-- 页面头部 --> <header> <h1>欢迎来到我们的餐厅</h1> <img src="logo.png" alt="餐厅Logo"> <nav> <ul> <li><a href="#home">首页</a></li> <li><a href="#menu">菜单</a></li> <li><a href="#contact">联系我们</a></li> </ul> </nav> <form id="search-form"> <input type="text" id="search-input" placeholder="请输入菜品名称或关键词"> <button type="submit">搜索</button> </form> </header> <!-- 查询结果展示区 --> <section id="dish-results"> <h2>查询结果</h2> <div class="dish-item"> <img src="dish1.jpg" alt="菜品图片"> <span class="dish-name">宫保鸡丁</span> <span class="dish-price">¥28</span> </div> <div class="dish-item"> <img src="dish2.jpg" alt="菜品图片"> <span class="dish-name">鱼香肉丝</span> <span class="dish-price">¥30</span> </div> </section> <!-- 分页功能 --> <footer> <ul class="pagination"> <li><button onclick="prevPage()">上一页</button></li> <li><button onclick="nextPage()">下一页</button></li> </ul> </footer> </body> </html> ``` 上述代码中,`<header>` 元素用于放置页面标题、Logo以及导航菜单[^2]。同时,`<form>` 标签内的输入框按钮构成了简单的搜索表单,允许用户输入关键词进行查询。 #### JavaScript交互处理 为了实现动态的菜品查询功能,可以结合JavaScript对用户的输入做出响应。以下是一个简单的交互逻辑实现: ```javascript document.getElementById('search-form').addEventListener('submit', function(event) { event.preventDefault(); // 阻止表单默认提交行为 const query = document.getElementById('search-input').value; fetchDishes(query); // 调用函数发送异步请求 }); function fetchDishes(query) { fetch(`/api/search?q=${encodeURIComponent(query)}`) .then(response => response.json()) .then(data => displayDishes(data)) .catch(error => console.error('Error:', error)); } function displayDishes(dishes) { const resultsContainer = document.getElementById('dish-results'); resultsContainer.innerHTML = '<h2>查询结果</h2>'; dishes.forEach(dish => { const dishItem = document.createElement('div'); dishItem.className = 'dish-item'; dishItem.innerHTML = ` <img src="${dish.image}" alt="菜品图片"> <span class="dish-name">${dish.name}</span> <span class="dish-price">¥${dish.price}</span> `; resultsContainer.appendChild(dishItem); }); } ``` 上述代码通过监听表单提交事件,捕获用户输入的查询条件,并调用 `fetchDishes` 函数向后端发送请求。返回的数据经过解析后,利用 `displayDishes` 函数动态生成菜品列表并插入到页面中。 #### CSS样式设计 为了提升用户体验,可以通过CSS对页面元素进行美化: ```css header { background-color: #f4f4f4; padding: 20px; text-align: center; } header img { width: 100px; height: auto; } #search-form { margin-top: 20px; } .dish-item { border: 1px solid #ccc; padding: 10px; margin: 10px 0; display: flex; align-items: center; } .dish-item img { width: 80px; height: auto; margin-right: 10px; } .dish-name { font-weight: bold; } .pagination button { margin: 5px; padding: 5px 10px; } ``` #### 数据库与后端支持 后端需要提供一个API接口,接收前端传来的查询参数,并根据条件从数据库中检索相关数据。例如,使用SQL语句实现模糊匹配查询[^3]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值