<a> 标签 href和onclick 同时使用的问题

本文探讨了HTML中a标签的事件触发顺序问题,特别是在IE6和其他浏览器中的差异。文章还讨论了如何通过不同方式设置链接和点击事件来避免某些浏览器的问题。
1 顺序
ie 6 : href 先触发 onclick 后触发
其他浏览器 先触发onlick 后触发 href

2 href="javascript: xxx()"
不能传入this作为参数
onclick可以
<a href="javascript:alert('href event');" onclick="clickevent(this);">

3 优先触发的方法如果返回 false 导致后一个事件不被触发
比如
<a href="javascript:alert('href event');" onclick="clickevent(this); return false;">

4
<a href="#"> 会导致页面定位到书签位置,

5
由于 1和 4 的原因
在ie6 下 同时有 <a href="#" 和 onclick的时候 由于页面先因为href重新载入了一次,导致 onclick事件被浏览器丢弃。


6 总结:
1) 在不需要传递this作为方法的参数时候,推荐
只使用href="JavaScript: "

2) 如果需要使用this参数,推荐
<a href="javascript:void(0);" onclick="doSomthing(this)" >
### 网上点餐系统中菜品查询操作的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]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值