简体   繁体   English

在父div中添加类

[英]Add class in parent div

<div class="parent"> <input type="text" /> </div>

$('.parent > *')
    .focus(function() {
        $('.parent').addClass('focused');
    })
    .blur(function() {
        $('.parent').removeClass('focused');
    });

I'm trying to add a class in a div only if the input field has value. 我试图仅在输入字段具有值的情况下才在div中添加一个类。 I mean if we put text in input field the div automatically should add a class it's self. 我的意思是,如果我们在输入字段中输入文本,div会自动添加一个自类。 Bu the only problem is we can't give any class name or Id to input. 唯一的问题是我们不能给任何类名或ID输入。

Using this script about we can add class when we click on input. 使用此脚本,我们可以在单击输入时添加类。 But we need to do more. 但是,我们需要做更多的事情。

Can we do that? 我们可以做到吗?

You can use input instead of focus . 您可以使用input代替focus You also have to check the value to add/remove the class. 您还必须检查值以添加/删除类。 Try the following way: 请尝试以下方式:

 $(document).ready(function(){ $('.parent > *').focus(); $('.parent > *') .on('input', function() { if($(this).val().trim() != '') $(this).parent().addClass('focused'); else $(this).parent().removeClass('focused'); }); }); 
 .focused{ color:red; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="parent">Test <input type="text" /> </div> 

With the html you provided, this should work 使用您提供的html,它应该可以正常工作

$('.parent > *')
.focus(function() {
   if( $(this).val().length) $(this).parent().addClass('focused');
})
.blur(function() {
    if( $(this).val().length) $(this).parent().removeClass('focused');
});

Update after OP comment OP评论后更新

<div class="some-div">Hello</div>
<div class="parent"> <input type="text" /> </div>

$('.parent > *')
.focus(function() {
    $(".some-div").addClass('focused');
})
.blur(function() {
    $('.some-div').removeClass('focused');
});

Sure, just use .parent > input : 当然,只需使用.parent > input

$('.parent > input')
    .focus(function() {
        $('.parent').addClass('focused');
    })
    .blur(function() {
        $('.parent').removeClass('focused');
    });

Here input event fires on Keyboard input, Mouse Drag, Autofill, and Copy-Paste. 在此, input事件在键盘输入,鼠标拖动,自动填充和复制粘贴时触发。

You can try below code - 您可以尝试以下代码-

 function toggleParentClass(elem) { console.log(elem.val()); if (elem.val().length) elem.closest('.parent').addClass('focused'); else elem.closest('.parent').removeClass('focused'); } $('.parent').on('input', function() { toggleParentClass($(this).find('input')); }); 
 .focused { background-color: red; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="parent"> <input type="text" /> </div> 

How about putting a event-listener on the input field. 如何在输入字段上放置事件侦听器。

If the input box goes empty, remove the class. 如果输入框为空,请删除该类。 Otherwise, add the required class. 否则,添加所需的类。

Closest helps you find the closest member with the given selection criteria. 最接近可帮助您找到具有给定选择标准的最接近成员。

For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree. 对于集合中的每个元素,通过测试元素本身并在DOM树中遍历其祖先,获得与选择器匹配的第一个元素。

$('input').change(function(){
     if( $(this).val().trim() === '' ){//Empty input box
        $(this).closest('.parent').removeClass('focused');
     }
     else{
        $(this).closest('.parent').addClass('focused');
     }
  }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM