只更改被点击的元素的样式?

时间:2013-09-27 12:59:48

标签: javascript jquery html

大家好,我一直在制作这个有不同输入的形式,但它们都有相同的类:

<input type="text" name="username" class="field" /> 
<input type="text" name="email" calss="field" />  

我想要做的是当点击输入字段时我想用JQuery更改边框颜色(只有同时点击的元素不是全部)

任何人都有想法?

3 个答案:

答案 0 :(得分:2)

<input type="text" name="username" class="field" /> 
<input type="text" name="email" calss="field" />  




$('.field').click(function(){
     $(this).css('attributeName','value'); //here $(this) represents current element.
});

答案 1 :(得分:2)

将点击事件绑定到所有输入,然后使用$(this)来定位实际点击的那个。

$('.field').on('click', function() {
     $('.field').removeClass('clicked'); // Remove previous
     var $this = $(this);
     $this.addClass('clicked'); // If you want to add the CSS with a class, which i recommend.
     $this.css('border', '[css-border-values]'); // Inline CSS
});

答案 2 :(得分:0)

<input type="text" name="username" class="field" /> 
<input type="text" name="email" calss="field" /> 

$('input[type=text]').focus(function(){
     $('input[type=text]').css({'border':''});
     $(this).css({'border':'solid 2px #ccc'});
});

http://jsfiddle.net/jCpfH/