这对某些人来说应该很容易,我似乎无法正确理解语法。我有以下代码,我确信70%的代码可以用循环表示:有人可以开导我吗?
function AddNewEmail(){
var jFilesContainer = $( "#emails" );
var jUploadTemplate = $( "#email-templates div.template" );
var jUpload = jUploadTemplate.clone();
var strNewHTML = jUpload.html();
var intNewFileCount = (jFilesContainer.find( "div.template" ).length + 1);
jUpload.attr( "id", ("emailedit[" + intNewFileCount + "]") );
strNewHTML = strNewHTML
.replace(
new RegExp( "::FIELD1::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD2::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD3::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD4::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD5::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD6::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD7::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD8::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD9::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD10::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD11::", "i" ),
intNewFileCount
)
.replace(
new RegExp( "::FIELD12::", "i" ),
intNewFileCount
)
;
jUpload.html( strNewHTML );
jFilesContainer.append( jUpload );
}
答案 0 :(得分:2)
如果您使用正则表达式,请使用它们:
strNewHTML = strNewHTML.replace(/::FIELD\d{1,2}::/gi, intNewFileCount);
答案 1 :(得分:2)
我会说
strNewHTML = strNewHTML.replace(/::FIELD\d+::/gi, intNewFileCount);
可以替代整个strNewHTML逻辑。不是循环,而是更短。
答案 2 :(得分:0)