简体   繁体   English

鼠标单击时文本输入更改类

[英]text input change class on mouse click

<input type="text" onkeypress="return buyukHarf(event);" class="green" name="f1"/>

is it possible to change the class green to something else when the area is clicked? 单击该区域时是否可以将green类别更改为其他类别? Or any other way to change the border color when clicked? 还是单击时更改边框颜色的任何其他方式? Thanks in advance... 提前致谢...

$("input").click(function(){
    $(this).removeClass("green").addClass("yellow");    
});

In the case of form elements, I'd use focus() and blur() over a click() handler. 对于表单元素,我将在click()处理函数上使用focus()blur()

Also, try to avoid using inline javascript as you have done in your example. 另外,请尝试避免像示例中那样使用内联JavaScript。

Focus/Blur example: 焦点/模糊示例:

$("input").focus(function(){
    $(this).removeClass("green").addClass("yellow");
}).blur(function(){
    $(this).removeClass("yellow").addClass("green");
});

Try this: 尝试这个:

$( "input" ).click( function() {
    $( this ).attr( { 'class' : 'anotherClass' } );
} );

Try: 尝试:

$("input[name='f1']").click(function(){
    $(this).removeClass("green").addClass("red");    
});

OR // for border 或//用于边框

$("input[name='f1']").click(function(){
    $(this).removeClass("green").css("border", "1px solid red");    
});

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

相关问题 如何在鼠标单击上更改元素文本 - How to Change Element Text on Mouse Click 在每次单击鼠标时更改元素类 - change class of element, on each mouse click 单击一个按钮以更改文本输入的文本 - Click a button to change the text of text input 文本 - 鼠标悬停在单词上 - 更改颜色,单击单词 - 播放音频,鼠标移出 - 更改文本颜色 - Text - on mouse over an word - change the color, on click over the word - play audio, on mouse out - change text color 使用Javascript将鼠标单击坐标插入文本输入框 - Insert Mouse Click Coordinates into text input box with Javascript 在Firefox中单击鼠标时无法选择所有输入元素文本 - Having trouble selecting all input element text on mouse click in Firefox JavaScript:从鼠标单击获取输入文本位置(以字符为单位) - JavaScript: Get input text position (in chars) from mouse click 是否可以将鼠标单击调用事件()到<input type = text>元素? - Is it possible to dispatchEvent() a mouse click to a <input type=text> element? 输入文本字段不会在鼠标单击时移动光标? - Input Text fields don't move cursor on mouse click? 使用jQuery鼠标单击事件永久更改DIV标签中的文本 - Using jQuery mouse click event to permanently change text in a DIV tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM