javascript - 使除下拉列表之外的整个 DIV 可点击
问题描述
我正在尝试使整个 DIV 可点击并使其正常工作。但是,我想对下拉菜单做一个例外。单击此下拉菜单button
不应打开next.html
。它目前打开下拉菜单,但也很快打开next.html
。我该怎么做?
演示:https://jsfiddle.net/40wntLyo/
<div class="project__body" onclick="location.href='next.html';">
<div class="row">
<div class="col-10">
<h1>Title of the DIV</h1>
</div>
<div class="col-2 text-right">
<div class="dropdown">
<button type="button" class="dropdown-btn dropdown-toggle" data-toggle="dropdown">Menu</button>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="#">Edit</a>
<a class="dropdown-item" href="#">Delete</a>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<p>There's more content here in this div. There's more content here in this div. There's more content here in this div. </p>
</div>
</div>
解决方案
首先,您必须删除内联 javascript 事件并尝试以下示例:
jQuery:
$('.dropdown').click(function(event){
event.stopPropagation();
$('.dropdown-menu').toggle();
})
$('.project__body').click(function(){
window.location.href="next.html"
})
html:
<div class="project__body">
<div class="row">
<div class="col-10">
<h1>Title of the DIV!</h1>
</div>
<div class="col-2 text-right">
<div class="dropdown">
<button type="button" class="dropdown-btn dropdown-toggle" data-toggle="dropdown">Menu</button>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="#">Edit</a>
<a class="dropdown-item" href="#">Delete</a>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<p>There's more content here in this div. There's more content here in this div. There's more content here in this div. </p>
</div>
</div>
推荐阅读
- android - Android ble扫描蓝牙附近的设备kotlin
- react-native - 如何使用 reducer 在 react-native 中正确发送数据?
- wordpress - Nginx 不将 add_header 指令应用于 WordPress JS、CSS、图像资源文件 URL 的标题
- python - 根据列的年月日映射具有不同行数的两个数据框
- angular - 具有对象内部动态参数的角度“接口”,但固定在“已实现”
- spring-boot - 使用 flyway-test-extension 创建名称为“flywayInitializer”的 bean 时出错
- intellij-idea - IntelliJ 2020.3 导出设置错误 - 写入文件
- java - JNI,如何在本机代码中访问正确的对象实例
- javascript - FabricJS:使用 Image 子类时无法执行“drawImage”
- android - android:autoMirrored 不适用于矢量可绘制对象