我该如何简化它
How do I simplify this?
本文关键字:何简化 更新时间:2023-09-26
我是JS的新手,正在自学。我想知道如何简化它?这种块在我的脚本中重复了太多次。
$('.itemlist').on('focus', 'textarea.remarks', function (){
$(this).animate({height: '50px'},400);});
$('.itemlist').on('blur', 'textarea.remarks', function (){
$(this).animate({height: '15px'},400);});
干杯。
您也可以这样做:
$('.itemlist').on({
focus: function(){
$(this).animate({ height: '50px' }, 400);
},
blur: function(){
$(this).animate({ height: '15px' }, 400);
}
}, 'textarea.remarks');
并不是说它更简单,但它看起来很漂亮
试试吧,
var height='50px';
$('.itemlist').on('focus blur', 'textarea.remarks', function (){
$(this).animate({height: height},400);
height=(height=='50px') ? '15px' : '50px';
});
也许类似于:
$('.itemlist').on('focusin focusout', 'textarea.remarks', function(evt)
{
var px = 0;
if(evt.type === 'focusin') px = 50;
if(evt.type === 'focusout') px = 15;
$(this).animate({ height: px }, 400);
});
更新:根据以下评论中的信息使用focusin
和focusout
。
相关文章:
- 可以简化嵌套的延迟Q Promises解析吗
- 将多个布尔检查简化为一个布尔检查
- 创建一个循环来简化HTML和CSS代码
- 使用element简化onclick函数
- “createImageData()”和“new ImageData(()”有何不同
- 尝试简化检查对象键是否为true并将其推送到另一个对象
- 在Scroll上更新CSS代码:简化它就不会了'不要使用JQuery和最简单的Javascript
- Javascript,简化了对象转换
- 如何简化动画jQuery代码
- 简化了点击功能的长列表
- 如何简化jquery动画函数代码
- 在JavaScript中简化开关案例
- 如何使用角度和表单数据简化发送(文件、模型)
- 通过找到一种删除许多 if 语句的方法来简化代码
- 简化两个 jquery 代码
- 这与 $(document).ready() 有何不同
- 简化复杂的if语句
- 需要简化这一长javascript行
- 纯javascript的简化效果
- XUL文档与HTML文档对象有何不同