简体   繁体   English

表格li背景变更

[英]Form li background change

I'm trying to achieve the effect of the li tag changing background colors when the user focuses on the input (effect can be seen at the bottom of the page here . From seeing similar questions it seems like it can't be done in pure CSS, so I was wondering how to do it in jquery (I have no knowledge). Here is my HTML: 我试图在用户专注于输入时实现li标记更改背景颜色的效果(效果可以在这里在页面底部看到。从看到类似的问题来看,似乎不能单纯地做到这一点CSS,所以我想知道如何在jquery中执行此操作(我不知道),这是我的HTML:

<form class="contact-form" action="" method="post" name="contact-form">  
        <ul>   
            <li>
                <input type="text" name="name" placeholder="NAME" class="test"/>
            </li>
            <li>
                <input type="email" name="email" placeholder="EMAIL" />
            </li>
            <li>
                <textarea name="message" name="message" placeholder="MESSAGE"></textarea>
            </li>
            <li>
                <button class="submit" type="submit">Send</button>
            </li>
        </ul>
    </form> 

Here's how to do it. 这是操作方法。 It will add a background color to the parent LI of the form field that has focus, and after exiting the field the background color will be removed. 它将为具有焦点的表单字段的父级LI添加背景色,退出该字段后,背景色将被删除。

Working example on jsFiddle . jsFiddle上的工作示例

CSS: CSS:

.selected {
    background: lightYellow;
}

jQuery: jQuery的:

$(function () {
    var $form = $(".contact-form"),
        selectedClass = "selected";

    $form.on("focus", "input, textarea", function () {
        $(this).closest("li")
            .addClass(selectedClass);
    });

    $form.on("blur", "input, textarea", function () {
        $(this).closest("li")
            .removeClass(selectedClass);
    });
});

You can use the :hover selector in your CSS statement; 您可以在CSS语句中使用:hover选择器; no need for JS at all.. 完全不需要JS。

.contact-form ul li { background-color:#000000; }
.contact-form ul li:hover { background-color: #C0C0C0; }

Heres a basic jquery version which activates on focus like example 这是一个基本的jQuery版本,可以像例子一样激活焦点

$('.contact-form').on('focus','input,textarea',function() {
    $('.contact-form li').animate({'backgroundColor':'#00FF00'});

    $(this).parent().animate({'backgroundColor':'#FF0000'});
});

fiddle 小提琴

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

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