HTML tag lt;agt; want to add both href and onclick working(HTML 标签 lt;agt;想要同时添加 href 和 onclick 工作)
问题描述
我想问一下HTML标签
I'd like to ask about HTML tag
<a href="www.mysite.com" onClick="javascript.function();">Item</a>
如何使这个 a 标签与 href 和 onClick 一起使用?(最好先运行 onClick 再运行 href)
How to make this a tag working with href and onClick? (prefer onClick running first then href)
推荐答案
你已经有了你需要的东西,只需要稍微改变一下语法:
You already have what you need, with a minor syntax change:
<a href="www.mysite.com" onclick="return theFunction();">Item</a>
<script type="text/javascript">
function theFunction () {
// return true or false, depending on whether you want to allow the `href` property to follow through or not
}
</script>
<a>
标签的onclick
和href
属性的默认行为是执行onclick
,然后只要 onclick
不返回 false
就按照 href
,取消事件(或事件未被阻止)
The default behavior of the <a>
tag's onclick
and href
properties is to execute the onclick
, then follow the href
as long as the onclick
doesn't return false
, canceling the event (or the event hasn't been prevented)
这篇关于HTML 标签 <a>想要同时添加 href 和 onclick 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:HTML 标签 <a>想要同时添加 href 和 oncl


- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06