通过组合这些函数防止javascript中的代码复制
所以我有两个jquery函数基本上做相同的事情,但是在两个不同的容器上。有没有办法将这两种功能结合起来通过组合这些函数防止javascript中的代码复制,javascript,optimization,Javascript,Optimization,所以我有两个jquery函数基本上做相同的事情,但是在两个不同的容器上。有没有办法将这两种功能结合起来 $('#autobid-list .auction-button').click( function(){ if($(this).attr('summary') == 'autobid-button'){ $(this).parents('li').addClass('none'); }else if($(this).attr('
$('#autobid-list .auction-button').click(
function(){
if($(this).attr('summary') == 'autobid-button'){
$(this).parents('li').addClass('none');
}else if($(this).attr('summary') == 'watchlist-button'){
if($(this).hasClass('watchlist-1')){
$(this).parents('li').clone().appendTo('#watchlist-list');
}else{
$('#watchlist-list .auction-' + $(this).parents('table').attr('summary')).parents('li').addClass('none');
}
}
}
);
$('#watchlist-list .auction-button').click(
function(){
if($(this).attr('summary') == 'watchlist-button'){
$(this).parents('li').addClass('none');
}else if($(this).attr('summary') == 'autobid-button'){
if($(this).hasClass('autobid-1')){
$(this).parents('li').clone().appendTo('#autobid-list');
}else{
$('#autobid-list .auction-' + $(this).parents('table').attr('summary')).parents('li').addClass('none');
}
}
}
);
这两者基本上是相同的,唯一的变化是输入“autobid”或“watchlist”当然,只需确定单击了哪一个,并根据该值分配变量即可。使用逗号分隔多个元素时,可以同时选择它们:
$('#watchlist-list .auction-button, #autobid-list .auction-button').click(function() {
// Determine which button was clicked, you might want to adjust this as needed.
var type = $(this).closest('#autobid-list').length > 0 ? 'autobid' : 'watchlist';
// Then just replace 'autobid' and 'watchlist' with 'type'
if($(this).attr('summary') == type+'-button') {
$(this).parents('li').addClass('none');
} else if($(this).attr('summary') == type+'-button') {
if($(this).hasClass(type+'-1')) {
$(this).parents('li')
.clone()
.appendTo('#'+type+'-list');
} else {
$('#'+type+'-list .auction-' + $(this).parents('table').attr('summary'))
.parents('li')
.addClass('none');
}
}
});
当然,只需确定单击了哪一个,并根据它分配一个变量。使用逗号分隔多个元素时,可以同时选择它们:
$('#watchlist-list .auction-button, #autobid-list .auction-button').click(function() {
// Determine which button was clicked, you might want to adjust this as needed.
var type = $(this).closest('#autobid-list').length > 0 ? 'autobid' : 'watchlist';
// Then just replace 'autobid' and 'watchlist' with 'type'
if($(this).attr('summary') == type+'-button') {
$(this).parents('li').addClass('none');
} else if($(this).attr('summary') == type+'-button') {
if($(this).hasClass(type+'-1')) {
$(this).parents('li')
.clone()
.appendTo('#'+type+'-list');
} else {
$('#'+type+'-list .auction-' + $(this).parents('table').attr('summary'))
.parents('li')
.addClass('none');
}
}
});
这行吗?生成一个返回函数的函数:
function MyFunction(type) {
return function(){
if($(this).attr('summary') == type + '-button'){
$(this).parents('li').addClass('none');
}else if($(this).attr('summary') == type + '-button'){
if($(this).hasClass(type + '-1')){
$(this).parents('li').clone().appendTo('#' + type + '-list');
}else{
$('#' + type + '-list .auction-' + $(this).parents('table').attr('summary')).parents('li').addClass('none');
}
}
}
}
$('#autobid-list .auction-button').click(MyFunction('autobid'));
$('#watchlist-list .auction-button').click(MyFunction('watchlist'));
这行吗?生成一个返回函数的函数:
function MyFunction(type) {
return function(){
if($(this).attr('summary') == type + '-button'){
$(this).parents('li').addClass('none');
}else if($(this).attr('summary') == type + '-button'){
if($(this).hasClass(type + '-1')){
$(this).parents('li').clone().appendTo('#' + type + '-list');
}else{
$('#' + type + '-list .auction-' + $(this).parents('table').attr('summary')).parents('li').addClass('none');
}
}
}
}
$('#autobid-list .auction-button').click(MyFunction('autobid'));
$('#watchlist-list .auction-button').click(MyFunction('watchlist'));
显而易见的答案:
$('#watchlist-list .auction-button').click(function(){
whatever.call(this,'watchlist', 'autobid');
});
$('#autobid-list .auction-button').click(function(){
whatever.call(this,'autobid', 'watchlist');
});
function whatever(one, two){
if($(this).attr('summary') == one + '-button'){
$(this).parents('li').addClass('none');
}else if($(this).attr('summary') == two + '-button'){
if($(this).hasClass(two + '-1')){
$(this).parents('li').clone().appendTo('#' + two + '-list');
}else{
$('#' + two + '-list .auction-' + $(this).parents('table').attr('summary')).parents('li').addClass('none');
}
}
}
尽管重新思考可能更好
编辑:Oops,已修复。显而易见的答案:
$('#watchlist-list .auction-button').click(function(){
whatever.call(this,'watchlist', 'autobid');
});
$('#autobid-list .auction-button').click(function(){
whatever.call(this,'autobid', 'watchlist');
});
function whatever(one, two){
if($(this).attr('summary') == one + '-button'){
$(this).parents('li').addClass('none');
}else if($(this).attr('summary') == two + '-button'){
if($(this).hasClass(two + '-1')){
$(this).parents('li').clone().appendTo('#' + two + '-list');
}else{
$('#' + two + '-list .auction-' + $(this).parents('table').attr('summary')).parents('li').addClass('none');
}
}
}
尽管重新思考可能更好
编辑:Oops,修复。啊,Sorpigal的回答是正确的,因为我没有意识到你在同一个函数中使用了两种类型是的,这很好,但有两种不同的类型,谢谢你的帮助thoAh,Sorpigal的回答是正确的,因为我没有意识到你在同一个函数中使用了两种类型是的,这很好,但是有两种不同的类型,谢谢你的帮助。实际上有两种不同:autobid或watchlist和第一个条件,另一种。实际上有两种不同:autobid或watchlist和第一个条件,另一种。这不起作用,因为有两种不同的类型,谢谢你的努力这将不起作用,因为有两种不同的类型,谢谢你的努力