Is there a preferred way of formatting jQuery chains to make them more readable?

前端 未结 4 692
粉色の甜心
粉色の甜心 2020-12-13 20:40

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

$(\"#FundTable\").append(
    objButton.paren         


        
4条回答
  •  庸人自扰
    2020-12-13 21:09

    How about:

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

    I find that chaining, when used in moderation, can lead to better readability.

提交回复
热议问题