是否有一种格式化jQuery链的首选方法,使它们更具可读性? [英] Is there a preferred way of formatting jQuery chains to make them more readable?

查看:89
本文介绍了是否有一种格式化jQuery链的首选方法,使它们更具可读性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下示例代码克隆表行,设置一些属性然后将其附加到表中:

Given this following sample code which clones a table row, sets some properties and then appends it to a table:

$("#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()
);

有没有人有任何关于如何将其格式化为更容易的建议?这样做是否有任何公认的约定?

Does anyone have any suggestions as to how this might be formatted to be easier on the eye? Is there any accepted convention for doing this?

有一套可以遵循的规则并且可以合并到一组标准中是很有用的。

It would be useful to have a set of rules that can be followed, and that can be incorporated into a set of standards.

推荐答案

我会重构这个。我发现眼睛上有3种以上的链接方法感到不安

I would refactor to this. I find more than 3 chained methods uneasy on the eye

       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");

这篇关于是否有一种格式化jQuery链的首选方法,使它们更具可读性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆