Initial status:
var searchMain = $('.c-search_search'), // 搜索本体
searchBtn = $('.c-search_searchBtn'), // 取消按钮
searchIcon = $('.weui_icon_search'); // 搜索图标
if (searchMain.val()) {
searchIcon.addClass('weui_icon_search-focus');
}
searchMain.on('focus', function () {
searchBtn.show();
searchIcon.addClass('weui_icon_search-focus');
}).on('blur', function () {
if (!this.value) {
searchBtn.hide();
searchIcon.removeClass('weui_icon_search-focus');
}
});
1. Obviously the most repeated class name is weui_icon_search-focus. So would it be better to save variables? If you need to modify it, just modify it
var searchMain = $('.c-search_search'), // 搜索本体
searchBtn = $('.c-search_searchBtn'), // 取消按钮
searchIcon = $('.weui_icon_search'), // 搜索图标
searchIconFocus = 'weui_icon_search-focus';
if (searchMain.val()) {
searchIcon.addClass(searchIconFocus);
}
searchMain.on('focus', function () {
searchBtn.show();
searchIcon.addClass(searchIconFocus);
}).on('blur', function () {
if (!this.value) {
searchBtn.hide();
searchIcon.removeClass(searchIconFocus);
}
});
2.focus blur These two events actually have almost the same operation behavior, so do they need to be encapsulated? In this case, we can also solve the problem of weui_icon_search-focus written in multiple places
var searchMain = $('.c-search_search'), // 搜索本体
searchBtn = $('.c-search_searchBtn'), // 取消按钮
searchIcon = $('.weui_icon_search'), // 搜索图标
searchIconFocus = 'weui_icon_search-focus';
if (searchMain.val()) {
searchIcon.addClass(searchIconFocus);
}
searchMain.on('focus', function () {
fn();
}).on('blur', function () {
if (!this.value) {
fn(true);
}
});
function fn(a) {
searchBtn[ a ? 'hide' : 'show' ]();
searchIcon[ a ? 'removeClass' : 'addClass' ]('weui_icon_search-focus');
}
But the operation of the position judged by if (searchMain.val()) cannot be helped. I think it should also be together with fn. After all, it is the same as one of the behaviors. If so, it needs to be changed. I changed it twice. Although I can add a parameter to fn so that it only executes the second sentence, I always feel uncomfortable doing this. If it is executed normally, it still has to go through that level of judgment~
Is there any perfect solution that you can’t find? ~
I can’t think of a better way. Personally, I think if the variable is a jquery object, $ can be added to distinguish it
var $searchMain = $('.c-search_search'), // Search ontology
if ($searchMain.val()) {
}
$searchMain.on('focus', function () {
}).on('blur', function () {
});
function fn(a,mark) {
}
You are encapsulating it for the sake of encapsulation. It is best for a function to only do one thing.
hide and show can also be packaged together with a toggle, but ordinary addClass is obviously different from this function.
You only need one command here, so there is no point in encapsulating them together. And if you have a similar requirement at this time, do you need to add another judgment to the function you encapsulated?
The purpose of encapsulating functions here is to enhance readability, but blindly encapsulating similar functions together is the opposite.
I agree with the answer above.
Your requirement is to display one element and add a class to another element when it gets focus. Losing focus is the opposite.
Then your implementation should be
The logic is clear and clear, why do we need unnecessary encapsulation?
If you must consider simplification, you should consider it from the perspective of displaying elements and adding a class style to the elements. For example, I add a class
show
to the parent element. To use the sub-selector in css to complete your problem of showing and hiding and style switching of the element.In this way, you only need to add the
.parent
这个元素添加show
样式,blur
style to the.parent
element when it is focused, and remove it when it isblur
.