简体   繁体   English

仅更改背景颜色

[英]Only change the background color

I want to be able to change the color of the form background color when clicked, but instead if i even click inside the form input box it also changes the form background. 我希望能够在单击时更改表单背景颜色,但是如果我什至在表单输入框内单击,它也会更改表单背景。

$('form').on('focus', 'input', function(e) {
    $(this).addClass('change');
  })

 $(document).on('click', 'input', function(e) { e.stopPropagation(); // this will prevent the bubble up to the click of form }).on('click', 'form', function(e) { $(e.currentTarget).toggleClass('change'); }) 
 form { height: 150px; width: 300px; background-color: #ccc; text-align: center; padding: 20px; } .change { background-color: lightgreen; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <label for="input"> Input <input type="text" value="" name="input" /> </label> </form> 

You can class your input and then check if user hasn't click in it, check it out: 您可以class的输入,然后检查,如果用户没有点击它,检查出来:

 $("form").click(function(e) { if(e.target.className != 'inputtext') { $(this).toggleClass('change'); } }) 
 form { background: yellow; } .change { background: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="form"> First name:<br> <input type="text" name="firstname" class="inputtext"><br> Last name:<br> <input type="text" name="lastname" class="inputtext"> </form> 

http://jsfiddle.net/UKhAn/167/ http://jsfiddle.net/UKhAn/167/

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

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