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

前端 未结 4 701
粉色の甜心
粉色の甜心 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:18

    Don't chain so much.

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

    Less chaining, but it seems easier to read imo.

提交回复
热议问题