javascript - 如何禁用特定div之外的点击

标签 javascript jquery html

这是示例代码。我想禁用搜索 id 之外的点击。就像我们需要在弹出窗口中禁用外部点击

<body>
You can search <a href="google.com">here</a>
<div id="search">
Search
<input type="text" name=search><button>search</button> 
</div>
</body>

最佳答案

您可以创建一个横跨整个屏幕的具有固定位置的 div,并将您希望能够点击的内容放在其中,使该元素之外的所有点击实际上都在该“空”div 上。

.disable-outside-clicks {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 10000;
}

.enabled-clicks {
  width: 200px;
  margin: 0 auto;
}
<div>
  <button>This button will not work</button>
</div>

<div class="disable-outside-clicks">
  <div class="enabled-clicks">
    <button>This button will work</button>
  </div>
</div>

关于javascript - 如何禁用特定div之外的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44152202/

相关文章:

javascript - 在 .js.erb 中处理 I18n

javascript - 将 Google Places API 限制在特定国家/地区

javascript - jQuery核心方法与实用程序方法

jquery - 对 jQuery $.get 和 $.load 感到困惑

javascript - 单击表标题内的 div 是单击父标题

html - CSS - 将图像集中在容器内

html - CSS <hr> 在文本旁边右对齐

javascript - 构建时的 ionic 错误

javascript - 如何处理 JSON 响应

html - HTML 和 CSS 的布局问题 - 由于其他元素的高度,无法将 div 紧贴地移动到新位置