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

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

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

提交回复
热议问题