javascript - anchor 标记内的 div onclick

标签 javascript html onclick anchor

我有这样的代码:

<a href="somewhere.html">
<img src="blabla">
<div class="wrap">
    <div id="id1" onclick="window.location = 'somepage1.html';"></div>
    <div id="id2" onclick="window.location = 'somepage2.html';"></div>
<div>
</a>

问题是 div 的 onclick 不起作用。当我点击 div 时,我会转到 somewhere.html。

最佳答案

您在链接中包含错误的 html 结构包装 div(除非使用 html5):

<a href="somewhere.html">
<img src="blabla">
<div class="wrap">
    <div id="id1" onclick="window.location = 'somepage1.html';"></div>
    <div id="id2" onclick="window.location = 'somepage2.html';"</div>
<div>
</a>

当您点击时,您将始终转到顶部的链接 somewhere.html 并且内部 div 不会执行您想要执行的操作。您应该修改您的 html 结构。

关于javascript - anchor 标记内的 div onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9342232/

相关文章:

javascript - 将 React 工具提示与样式化的组件一起使用

javascript - 如何使用动态 key 访问对象?

javascript - pouchdb put 仍然被 _rev 拒绝

java - IE 将 JAX-RS Web 服务的字符串响应视为文件

javascript - 如何将 iframe 视频的缩略图 100% 覆盖到 9 :16 aspect ratio? 中的包装器

javascript - 如何在 React 组件中通过 onclick 调用另一个函数内的函数?

android - 如何通过单击 ListView 上的项目从 ListView 中获取数据

JavaScript 1.6 Array.map() 和 Array.filter() 不使用内置函数作为参数

html - Bootstrap CSS,网格问题

javascript - OnClick 事件在 google chrome 和 safari 中不起作用