javascript - 具有 "left: 0px"的元素上的绑定(bind)事件

标签 javascript jquery html css

<span class="switchery switchery-small" style="box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset; border-color: rgb(223, 223, 223); transition: border 0.4s, box-shadow 0.4s; background-color: rgb(255, 255, 255);">
    <small style="left: 0px; transition: background-color 0.4s, left 0.2s;"></small>
</span>

我正在使用一个插件,它生成了上面的代码。如何选择左侧为 0px 的小标签的 switchery?

这是我写的

if ($('.switchery').find('small').attr('style').indexOf('0px') > -1) {

}

但是我如何应用 click() 我的 matches 元素。

最佳答案

使用filter带回调。

  1. 选择所有 <small>里面的元素.switchery元素
  2. 过滤具有left的元素属性(property)零

var smallEl = $('.switchery small').filter(function() {
  return parseInt($(this).css('left')) === 0;
}).css('color', 'green');

smallEl.click(function() {
  console.log('Hello');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<span class="switchery switchery-small" style="box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset; border-color: rgb(223, 223, 223); transition: border 0.4s, box-shadow 0.4s; background-color: rgb(255, 255, 255);">
  <small style="left: 0px; transition: background-color 0.4s, left 0.2s;">Left 0</small>
  <small style="left: 100px; transition: background-color 0.4s, left 0.2s;">Left 100</small>
  <small style="transition: background-color 0.4s, left 0.2s;">No Left</small>
</span>

关于javascript - 具有 "left: 0px"的元素上的绑定(bind)事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33558814/

相关文章:

javascript - 为什么当我手动重新加载页面时没有调用 Controller ?以及如何修复它?

javascript - 在 React 中过滤特定的表行

javascript - React/Redux - 无限滚动/从 API 加载更多

javascript new 关键字 - 返回内部匿名函数内创建的新对象

jQuery qrcode 不适用于 Internet Explorer 8

javascript - html中的表单提交触发器

Python Selenium 无法点击按钮(不在 iframe 内)

javascript - JQuery对象通过ajax向PHP发送邮件()

html - Footer 和 Jumbotron 之间的空白

javascript - 触发 Mouseout 事件取决于窗口大小