下面的示例代码克隆了一个表行,设置了一些属性,然后将其附加到表中:
$("#FundTable").append(
objButton.parents("tr").clone()
.find(".RowTitle").text("Row " + nAddCount).end()
.find(".FundManagerSelect").attr("id", "FundManager" + nAddCount)
.change(function() { ChangeFundRow(); }).end()
.find(".FundNameSelect").attr("id", "FundName" + nAddCount).end()
);
有没有人有任何建议,关于如何格式化这可能会更容易看起来?做这件事有什么公认的惯例吗?
如果有一组可以遵循的规则,并且可以将这些规则合并到一组标准中,这将是非常有用的。
发布于 2009-08-17 08:55:20
我会对此进行重构。我发现3种以上的连锁方法看起来很不舒服。
var $clonedRow = objButton.parents("tr").clone();
$clonedRow.find(".RowTitle")
.text("Row " + nAddCount);
$clonedRow.find(".FundManagerSelect")
.attr("id", "FundManager" + nAddCount)
.change( ChangeFundRow );
$clonedRow.find(".FundNameSelect")
.attr("id", "FundName" + nAddCount);
$clonedRow.appendTo("#FundTable");
发布于 2009-08-17 09:03:59
我缩进,就好像它是用括号括起来的:
$("#FundTable")
.append(objButton.parents("tr")
.clone()
.find(".RowTitle")
.text("Row " + nAddCount)
.end()
.find(".FundManagerSelect")
.attr("id", "FundManager" + nAddCount)
.change(function() {
ChangeFundRow(); // you were missing a semicolon here, btw
})
.end()
.find(".FundNameSelect")
.attr("id", "FundName" + nAddCount)
.end()
)
;
发布于 2009-08-17 08:53:19
不要做太多的链条。
var newContent = objButton.parents("tr").clone();
newContent.find(".RowTitle").text("Row " + nAddCount)
newContent.find(".FundManagerSelect").attr("id", "FundManager" + nAddCount)
.change(function() { ChangeFundRow() });
newContent.find(".FundNameSelect").attr("id", "FundName" + nAddCount);
$("#FundTable").append(newContent);
更少的链接,但它似乎更容易阅读imo。
https://stackoverflow.com/questions/1286829
复制相似问题