代码之家  ›  专栏  ›  技术社区  ›  sqlchild

jquery选择器,用于选择不属于指定类的所有文本输入

  •  0
  • sqlchild  · 技术社区  · 6 年前

    在下面的jquery代码中,我需要跳过一些类名不等于item的输入框

    输入[类型=文本][类!=item],这不起作用

    . 如何添加此异常?

         $(document)  
                        .on('focus', 'input[type=text]")', function() { 
                            $('.footer').css('position', 'absolute');
                            $('.footer').css('bottom', ''); 
                        })
    
                        .on('blur', 'input[type=text]', function() { 
                            $('.footer').css('position', 'fixed');  
                            $('.footer').css('bottom', '0');
                        });
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   K K    6 年前

    尝试

    $("input[type='text']:not('.classname')");
    

    因此,添加和修改代码:

    $(document)
      .on('focus', "input[type='text']:not('.classname')", function() {
        $('.footer').css({
          'position': 'absolute',
          'bottom': 'auto'
        });
      })
    
      .on('blur', "input[type='text']:not('.classname')", function() {
        $('.footer').css({
          'position': 'fixed',
          'bottom': 0
        });
      });
        2
  •  1
  •   Takit Isy    6 年前

    你想用 :not() 查询中的选择器:

    有关结果,请参见代码段:

    // 2 solutions here
    // Using the class attribute $("input[type=text]:not([class=item])")
    console.log($("input[type=text]:not([class=item])").length, "element matched.");
    
    // Using the class selector $("input[type=text]:not('.item')")
    console.log($("input[type=text]:not('.item')").length, "element matched.");
    
    
    // Using the code from your question:
    $("input[type=text]:not('.item')").on('focus', function() {
      $('.footer').css('position', 'absolute');
      $('.footer').css('bottom', '');
    })
    
    $("input[type=text]:not('.item')").on('blur', function() {
      $('.footer').css('position', 'fixed');
      $('.footer').css('bottom', '0');
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="text" class="item">
    <input type="text" class="item">
    <input type="text">

    我已经在代码片段中添加了你的代码,但我不知道你想用 .footer 元素。

    希望有帮助。