具有“ left:0px”的元素上的绑定事件

珍妮弗·安妮斯顿
<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的小标签的交换器?

我写了这个

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

}

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

杜沙尔

过滤器与回调一起使用

  1. 选择<small>元素中的所有.switchery元素
  2. 过滤left属性为零的元素

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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章