2012-06-04 30 views
11

Tôi có một biểu mẫu liên hệ. Tôi muốn thêm class vào div cha của hộp đầu vào trên input: focus. Đây là fiddle. Tôi muốn rằng không chỉ hộp đầu vào mà màu nền div sẽ chuyển sang màu tím ở tiêu điểm nhập.jquery thêm lớp cho phụ huynh vào tiêu điểm nhập

Markup:

<div class="round"> 
    <div class="round_label">name</div> 
    <input type="text" class="round_text"> 
</div> 

Javascript:

jQuery(document).ready(function($) { 
    $(".round_text:focus").parent().css('background', '#8b66ac'); 
    $('.round_text:focus').parent().css('background-color', 'red'); 
}); 

CSS:

.round { 
    background: none repeat scroll 0 0 #F3F3F3; 
    border-radius: 30px 30px 30px 30px; 
    border-top: 1px solid #B2B2B2; 
    height: 50px; 
    padding-left: 20px; 
    width: 440px; 
} 
.round_text:focus { 
    background: none repeat scroll 0 0 #8b66ac; 
} 
.round_label { 
    border-right: 1px solid #D1D1D1; 
    color: #B6B6B6; 
    float: left; 
    font-size: 16px; 
    height: 40px; 
    padding-top: 10px; 
    width: 156px; 
} 
.round_text { 
    background: none repeat scroll 0 0 #F3F3F3; 
    border: medium none; 
    color: #424242; 
    float: left; 
    height: 50px; 
    width: 240px; 
} 

Ngoài ra, bạn có thể giúp tôi như thể tôi cần phải thêm hai thuộc tính ví dụ:

$(".round_text").focus(function(){ 
     $(this).parent().css('background', '#8b66ac','color','#fff'); 

    }).blur(function(){ 
     $(this).parent().css('background', /*color*/); 
    }) 
+2

Luôn thêm mã của bạn trong câu hỏi, cùng với liên kết jsfiddle – Jashwant

+0

Cảm ơn đã gợi ý . Sẽ làm như vậy từ lần sau. – user930026

Trả lời

17

Bạn có thể thêm lớp để thay thế và cung cấp cho các phong cách

jQuery(document).ready(function($) { 

$(".text").focus(function(){ 
    $(this).parent().removeClass("round"); 
    $(this).parent().addClass("bluebg"); 

    }).blur(function(){ 
     $(this).parent().removeClass("bluebg"); 
    $(this).parent().addClass("round"); 
    }) 

});  

.bluebg { 
background: none repeat scroll 0 0 #8b66ac; 
color:#623886; 
} 
4

Hãy thử

$("input").on("focus",function(){ 
    $(this).parent().addClass("any_class").css("background","purple"); 
    }); 
+0

tại sao thêm lớp AND sử dụng phương thức css()? – charlietfl

+0

tôi đã đọc tiêu đề mà op muốn thêm lớp học cho phụ huynh và trong mô tả, ông muốn đặt nền thành màu tím do đó tôi đặt cả hai –

9

kiện Sử dụng tập trung này

$(".round_text").focus(function(){ 
     $(this).parent().css('background', '#8b66ac'); 

    }).blur(function(){ 
     $(this).parent().css('background', /*color*/); 
    }) 

EDIT: giải pháp thay thế sử dụng toggleClass()

CSS

.round_text:focus, .round.is_focused { 
    background: none repeat scroll 0 0 #8b66ac; 
} 

JS

$(".round_text").on('focus blur', function(){ 
    $(this).parent().toggleClass('is_focused'); 
}) 
+0

Câu trả lời hoàn hảo :) Cảm ơn –

3

Nếu tôi nhận được câu hỏi của bạn đúng,

Bạn đang chọn focused yếu tố, nhưng bạn cần phải thay đổi css của cha mẹ div trên focus sự kiện.

Làm điều này,

jQuery(document).ready(function($) { 

    $(".round_text").focus(function(){ 
     $(this).parent().css('background', '#8b66ac'); 
    }) 
}); 

Fiddle

Các vấn đề liên quan