合并jQuery事件方法的正确方法
Correct way to merge jQuery event methods
我正在尝试优化我的代码。
我目前有两个单独的调用w/一个代码块,每个代码块都做足够多的相同的事情。我知道,如果我正在查看具有相同事件方法(例如。click)的两个独立项目,我可以简单地在。
中添加额外的选择器。但是我同时使用on click和on change。
编辑:#walking和#driving是单选按钮,#narrow是选择框。这是我目前拥有的:
// When a place type is selected
$('#narrow').on('change', function() {
// Clear the current results
$('#place_results_wrap').empty();
// Show progress
$('#place_results_wrap').html('' +
'<div id="load_container">' +
'<div id="load">' +
'<i class="fa fa-refresh fa-spin fa-3x fa-fw"></i><span class="sr-only">Loading</span>' +
'<div id="status"><h3>Please wait...</h3></div>' +
'</div>');
// Temp disable the narrow
$("#narrow").prop("disabled", true);
setTimeout(function() {
$("#narrow").prop("disabled", false);
}, 15000);
// Grab the current transport method
var mode = $('input[name=travelmode]:checked').val().toLowerCase();
// Load page into results div
$('#place_results_wrap').load('/assets/php/places.php?type=' + this.value + '&mode=' + mode, '&latlon=' + latlon, function() {
$('html, body').animate({
scrollTop: $(".place_results").offset().top
}, 500);
});
});
// If the travel type is changed
$('#walking, #driving').click(function(){
// Grab the current place type
var place_type = $('select#narrow').val();
// If it has been specified
if(place_type)
{
// Clear the current results
$('#place_results_wrap').empty();
// Show progress
$('#place_results_wrap').html('' +
'<div id="load_container">' +
'<div id="load">' +
'<i class="fa fa-refresh fa-spin fa-3x fa-fw"></i><span class="sr-only">Loading</span>' +
'<div id="status"><h3>Please wait...</h3></div>' +
'</div>');
// Temp disable the travel type
$("#driving, #walking").prop("disabled", true);
setTimeout(function() {
$("#driving, #walking").prop("disabled", false);
}, 15000);
// Grab the current category
var type = $('select#narrow').val();
// Grab the current transport method
var mode = this.value.toLowerCase();
// Load page into results div
$('#place_results_wrap').load('/assets/php/places.php?type=' + type + '&mode=' + mode, '&latlon=' + latlon, function() {
$('html, body').animate({
scrollTop: $(".place_results").offset().top
}, 500);
});
}
});
是否有一种方法可以让我合并#narrow change和#walking, #driving click?
你可以这样做:
用change事件处理程序创建一个函数:
// When a place type is selected
function onChange() {
// Clear the current results
$('#place_results_wrap').empty();
// Show progress
$('#place_results_wrap').html('' +
'<div id="load_container">' +
'<div id="load">' +
'<i class="fa fa-refresh fa-spin fa-3x fa-fw"></i><span class="sr-only">Loading</span>' +
'<div id="status"><h3>Please wait...</h3></div>' +
'</div>');
// Temp disable the narrow
$("#narrow").prop("disabled", true);
setTimeout(function() {
$("#narrow").prop("disabled", false);
}, 15000);
// Grab the current transport method
var mode = $('input[name=travelmode]:checked').val().toLowerCase();
// Load page into results div
$('#place_results_wrap').load('/assets/php/places.php?type=' + this.value + '&mode=' + mode, '&latlon=' + latlon, function() {
$('html, body').animate({
scrollTop: $(".place_results").offset().top
}, 500);
});
}
$('#narrow').on('change', onChange);
在click事件中,只需调用为change事件定义的函数:
// If the travel type is changed
$('#walking, #driving').click(function(){
// Grab the current place type
var place_type = $('select#narrow').val();
// If it has been specified
if(place_type)
{
onChange();
}
});
代码是一样的,你只有一个检查
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- JQuery合并了keyup和focusout两个函数
- 数组在递归方法中设置为null
- javascript中是否存在可以合并多个对象的函数或方法
- 如何将两个方法合并为一个(.style + .getcomputed 样式)
- 在 JavaScript 中合并多个排序对象数组的最有效方法是什么?
- 按id合并两个阵列的有效方法,无重复
- 如何将jquery UI滑块与canvas rotate()方法合并
- 使用jQuery扩展方法将函数与对象合并,为什么'只返回s的函数
- 是否有一种方法可以使自定义HTML标记合并其他标记
- 将Javascript数组合并为Object Literal的最佳方法
- 将具有相同属性的两个对象的方法合并到每个属性的数组中
- 合并ES6地图/集合的最简单方法
- 如何将两个对象合并为包含方法的单个对象
- 合并jQuery事件方法的正确方法
- 是否有更短的方法来合并重复的对象
- 链接多个javascript文件而不缩小或合并到一个文档的方法
- Humble Haxe,在非 haxe 友好的目标项目中自动合并 haxe 的方法
- 是否有一种方法可以使用Javascript合并两个路径元素(svg)