[英]jQuery onfocus and onblur event minification
As you can see I am trying to increase the height of the text field when the user types into it and when he clicks away (element loses focus) I am making it smaller. 如您所见,当用户键入文本字段并且单击鼠标单击(元素失去焦点)时,我正在尝试增加文本字段的高度,我正在缩小文本字段的高度。
Is there a simpler way to write the following code? 有没有更简单的方法来编写以下代码?
Simple HTML input field: 简单的HTML输入字段:
<input type="text" />
jQuery: jQuery的:
$(document).ready(function() {
$('input').on('focus', function() {
$(this).height('20px');
});
$('input').on('blur', function() {
$(this).height('12px');
});
});
This can be done more-efficiently with just CSS, but using jQuery, you could use event delegation. 仅使用CSS可以更有效地完成此操作,但是使用jQuery,您可以使用事件委托。
$(document).on('focus blur', 'input', function(e) {
$(this).height(e.type === 'focusin' ? '20px' : '12px');
})
For completeness, here's the CSS solution: 为了完整起见,这是CSS解决方案:
input {
height: 12px;
}
input:focus {
height: 20px;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.