问题描述
我有以下jquery代码
I have following jquery code
$('a.editpo, a.resetpass').click(function(event){
event.preventDefault();
var urlToCall = $(this).attr('href');
var hyperlinkid = '#'+$(this).attr('id');
var targetId = $(this).attr('id').match(/\d+/);
var targetTrDiv = '#poformloadertr_'+targetId;
var targetTdDiv = '#poformloadertd_'+targetId;
var currentLink = $(this).html();
/*Todo: refactor or shorten the following statement*/
if((currentLink=='Edit' && $('#resetuserpassform_'+targetId).is(':visible'))
||
(currentLink=='Reset Pass' && $('#account-home-container-'+targetId).is(':visible'))
||
($(targetTdDiv).html() =='')
){
$.ajax({
url:urlToCall,
success: function(html){
$(targetTdDiv).html(html);
$(targetTrDiv).show();
}
});
}else{
$(targetTrDiv).hide();
$(targetTdDiv).html('');
}
});
editpo和resetpass是应用于表的列中的超链接的类,即Edit
和Reset Pass
,单击它们会在表行中加载表格,各个tr
和td
的ID为targetTrDiv
和targetTdDiv
.不太擅长JS,特别是jQuery,因此,如果您觉得可以在某个步骤进行代码优化,请随时这样做.但是我对减少条件语句特别感兴趣.此外,请指出我是否正确编写了这篇文章.谢谢!
The editpo and resetpass are classes applied on hyperlinks in column of table, namely Edit
and Reset Pass
, clicking these load up the form in a table row, ids for the respective tr
and td
is targetTrDiv
and targetTdDiv
. Am not good at JS and specially jQuery, hence if you feel that code can be optimised at some step please feel free to do so. But I am specially interested in reducing the condition statement. Moreover, please point me if I am correctly writing this or not. Thanks!
推荐答案
首先,您可以优化以下代码:
First, you can optimize the following code:
var urlToCall = $(this).attr('href');
var hyperlinkid = '#'+$(this).attr('id');
var targetId = $(this).attr('id').match(/\d+/);
var targetTrDiv = '#poformloadertr_'+targetId;
var targetTdDiv = '#poformloadertd_'+targetId;
var currentLink = $(this).html();
收件人:
var wrappedSet$ = $(this);
var urlToCall = wrappedSet$.attr('href');
var hyperlinkid = '#'+wrappedSet$.attr('id');
var targetId = wrappedSet$.attr('id').match(/\d+/);
var targetTrDiv = '#poformloadertr_'+targetId;
var targetTdDiv = '#poformloadertd_'+targetId;
var currentLink = wrappedSet$.html();
另外,您可以删除currentLink=='Edit' &&
和currentLink=='Reset Pass' &&
代码段,因为可以确保使用在jQuery中使用的类选择器单击了正确的链接.点击处理程序( a.editpo,a.resetpass ).
Also, you could remove the currentLink=='Edit' &&
and currentLink=='Reset Pass' &&
code snippets, because you can be sure that the right links were clicked using the class selector that you are using in your jQuery click handler (a.editpo, a.resetpass).
如果您这样做,则编码语句将保持如下所示:
If you do that the codition statement will stays like this:
if(($('#resetuserpassform_'+targetId).is(':visible'))
||
($('#account-home-container-'+targetId).is(':visible'))
||
($(targetTdDiv).html() =='')
){
/* AJAX call goes here */
}
此外,希望我是错的,条件语句不可能更优化.得出此结论的原因是,您需要非常特殊的特性,而这可能是其他方式无法实现的.
Besides, and hopefully I'm wrong, it's very unlikely that the condition statement can be more optimized. The reason for concluding this is that you want to much specificity that is likely impossible to achieve in other way.
希望它对您有帮助.
这篇关于帮助优化/重构代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!