javascript - 选择 div 内的所有元素,但不选择特定元素

标签 javascript jquery

如何选择 .content 而不是 .bts 中的所有元素?这是我的代码。我正在使用 not() 函数来排除 .bts 但它不起作用。应该做什么?

$('.content').not('.bts').click(function() {
  alert("hit");
});
.content {
  height: 100%;
  width: 100%;
}

p {}

.bts {
  background: blue;
  height: 100px;
  width: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">
  <p>test</p>
  <div class="bts">bts</div>
</div>

最佳答案

你可以使用$('.content *:not(.bts)')

.content * 表示它将获取 .content 上的所有元素,添加 not(.bts) 会删除带有 bts class 来自规则。

演示

$('.content *:not(.bts)').click(function() {
  console.log("hit");
});
.content {
  height: 100%;
  width: 100%;
}

p {}

.bts {
  background: blue;
  height: 100px;
  width: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">
  <p>test</p>
  <div class="bts">bts</div>
</div>

关于javascript - 选择 div 内的所有元素,但不选择特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56596218/

相关文章:

javascript - 在 HTML 中请求时用 PHP 生成 JavaScript 文件

javascript - 元素中的 polymer 定义属性

javascript - 在输入完成最大长度后防止不必要的光标

javascript - 使用 jquery 暂停 mediaelement.js

jquery - Fluid 960 Grid jQuery slideToggle

javascript - 如何将 dc.js 和 crossfilter.js html 页面放入 Angular 组件中?

javascript - 在 IE11 中使用 Angular 记录显示奇怪的行为

jQuery - UI slider 鼠标禁用和重新启用鼠标

javascript - 将 DIV 实现为链接

javascript - 如何将此 javascript 函数更改为 Jquery