String.prototype.format = String.prototype.f = function() { var s = this, i = arguments.length; while (i--) { s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); } return s; };
使用
//创建某table的一行 var rowTemplate = "<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td><input type=\"checkbox\" name=\"_selectedFile\" value=\"" + reuslt.fileName + "\" {5}/></td></tr>"; var disabled = "checked"; if (reuslt.status != "OK") { disabled = "disabled"; } var newRow = rowTemplate.format(reuslt.fileName, reuslt.size, reuslt.type, reuslt.version, reuslt.status, disabled); $('#uploadFileList tr:last').after(newRow);