[英]How can I write this more efficiently?
Whether or not this is the best way to do this, I don't care - but in this case I have 2 identical search bars which I toggle based on screen width. 不管这是否是执行此操作的最佳方法,我不在乎-但是在这种情况下,我有2个相同的搜索栏,可以根据屏幕宽度进行切换。 One shows for mobile, one shows for desktop.
一台显示移动设备,一台显示桌面设备。 The code below works well but there's so much repetition, there must be a better way to write it.
下面的代码运行良好,但是重复很多,必须有更好的编写方法。
The HTML: HTML:
<div class="search-wrp">
<input type="text" class="search" data-which="mobile" value="Search" />
<input type="button" value=" " class="go sprite" data-which="mobile" />
</div>
<div class="search-wrp">
<input type="text" class="search" data-which="desktop" value="Search" />
<input type="button" value=" " class="go sprite" data-which="desktop" />
</div>
The JS: JS:
$('.search').focus(function () {
var viewport = $(this).attr('data-which');
if ($('.search[data-which="' + viewport + '"]').val().toLowerCase() == "search") { $('.search[data-which="' + viewport + '"]').val(""); }
}).focusout(function () {
var viewport = $(this).attr('data-which');
if ($('.search[data-which="' + viewport + '"]').val() == "") { $('.search[data-which="' + viewport + '"]').val("Search"); }
}).keypress(function (event) {
var viewport = $(this).attr('data-which');
if (event.keyCode == '13') {
event.preventDefault();
$('.go[data-which="' + viewport + '"]').click();
}
});
$('.go').click(function () {
var viewport = $(this).attr('data-which');
if ($('.search[data-which="' + viewport + '"]').val() == null || $('.search[data-which="' + viewport + '"]').val() == "Search") {
alert("Please enter a search phrase.");
} else {
window.location = "http:" + "//" + window.location.host + "/Search.aspx?phrase=" + $('.search[data-which="' + viewport + '"]').val();
}
});
I would like to use a variable for '.search[data-which="' + viewport + '"]' and I'd also like to be able to define viewport just once in a way that still makes it usable within the functions. 我想为'.search [data-which =“'+视口+'”]'使用一个变量,并且我也希望能够只定义一次视口,并且仍然可以在函数中使用它。 However my attempts have all run into scope issues.
但是,我的尝试都遇到了范围问题。
Rather than repeatedly call $('.search[data-which="' + viewport + '"]')
inside each function, store it in a variable. 与其在每个函数内重复调用
$('.search[data-which="' + viewport + '"]')
,而是将其存储在变量中。 Eg: 例如:
$('.search').focus(function () {
var viewport = $(this).attr('data-which'),
search = $('.search[data-which="' + viewport + '"]');
if (search.val().toLowerCase() == "search") { search.val(""); }
}).focusout(function () {
var viewport = $(this).attr('data-which'),
search = $('.search[data-which="' + viewport + '"]');
if (search.val() == "") { search.val("Search"); }
}).keypress(function (event) {
var viewport = $(this).attr('data-which');
if (event.keyCode == '13') {
event.preventDefault();
$('.go[data-which="' + viewport + '"]').click();
}
});
$('.go').click(function () {
var viewport = $(this).attr('data-which'),
search = $('.search[data-which="' + viewport + '"]');
if (search.val() == null || search.val() == "Search") {
alert("Please enter a search phrase.");
} else {
window.location = "http:" + "//" + window.location.host + "/Search.aspx?phrase=" + search.val();
}
});
Always store the return value of a function if you need that value more than once. 如果多次需要该函数的返回值,请始终存储该返回值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.