提高设置兄弟属性的性能

时间:2013-10-31 16:09:42

标签: jquery function find

我有这个jquery代码,它按照我想要的方式工作。 选中子项时,它会突出显示行的颜色。但是,它很慢。 这是代码行:

chkStation.parent().parent().parent().parent().parent().closest("tr").siblings().find('td:contains(' + districtName + ')').attr('style','background-color:lime');  

chkStation.parent().parent().parent().parent().parent().closest("tr").siblings().find('td:contains(' + districtName + ')').siblings().attr('style','background-color:lime'); 

有没有人对如何加快上述代码有任何建议?

当用户使用之前帖子中的添加内容检查电台时,这是完整的功能(谢谢):

$("[id*=Station]").live("click", function () 
{
    var chkStation = $(this);

    if (chkStation.is(":checked"))   
    {
        //Set the station row to green
         chkStation.closest("tr").css('background-color', 'lime');

        var districtID = chkStation.parent().parent().parent().parent().parent().parent().attr('id'); 
        var districtName = districtID.substring(3);

        //Sets the district row green and checks the district checkbox
        var $stationDistrict = chkStation.parent().parent().parent().closest("tr").siblings().find('td:contains(' + districtName + ')');
        $stationDistrict.css("background-color", "lime" );  
        $stationDistrict.siblings().css( "background-color", "lime" );      

        //Checks the district checkbox
        var $districtCkBox = $stationDistrict.find('input:checkbox');
        $districtCkBox.attr("checked", true);

    }
    else 
    {
        //Sets the station row gray
         chkStation.closest("tr").css('background-color', '#DEDFDE');

        //If there are no checked stations under this district,
        //the district is unchecked and color is set to gray   
        //get the number of unchecked checkbox
        var numCkStations = $(this).closest("tr").find(":checkbox[checked='checked']").length;

        if(numCkStations == 0)
        {
            var districtID = chkStation.parent().parent().parent().parent().parent().parent().attr('id'); 
            var districtName = districtID.substring(3);

            var $stationDistrict = chkStation.parent().parent().parent().closest("tr").siblings().find('td:contains(' + districtName + ')');
            $stationDistrict.css("background-color", "#DEDFDE" );  
            $stationDistrict.siblings().css( "background-color", "#DEDFDE" );  

            //Unchecks the district checkbox
            var $districtCkBox = $stationDistrict.find('input:checkbox');
            $districtCkBox.attr("checked", false);
        }  
    }
});

有没有办法比现在更快地突出显示行?

以下是HTML ...按要求:

 <form id="form1" runat="server">   
     <cc1:CoolGridView ID="ParentGridView" runat="server" AllowSorting="True" OnSorting="GridView1_Sorting" 
        AutoGenerateColumns="False" OnRowDataBound="gv_RowDataBound"
        BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" 
        Height="550px" Width="525px" HorizontalAlign="Left" DefaultColumnWidth="140px">
     <HeaderStyle BackColor="#4A3C8C" ForeColor="#E7E7FF"></HeaderStyle>

    <Columns>
        <asp:TemplateField HeaderText="District" SortExpression="district" HeaderStyle-Width="200px">
            <ItemTemplate>
            <a href="javascript:DivExpandCollapse('div<%# Eval("district")%>');">
            <img id="imgdiv<%# Eval("district")%>" width="10px" border="0" src="Images/plus.png" /> </a> 
            <asp:CheckBox ID="District" Text='<%#Bind("district")%>' runat="server" />
            </ItemTemplate>
            <HeaderStyle Width="200px"></HeaderStyle>
            <ItemStyle HorizontalAlign="Center" Width="200px"></ItemStyle>
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="Server" HeaderStyle-Width="280px">
        <ItemTemplate>
        <asp:DropDownList DataTextField="Server" DataValueField="Server" ID="DropDownList2" Runat="server" >
           <asp:ListItem>A1</asp:ListItem>
           <asp:ListItem>A2</asp:ListItem>
           <asp:ListItem>Both</asp:ListItem>
        </asp:DropDownList>

        <tr><td colspan="100%">  
        <div id="div<%# Eval("district") %>" style="display:none; position: relative; left: 15px; overflow: auto">
            <asp:GridView ID="StationGridView" runat="server" AutoGenerateColumns="False"  OnSorting="GridView1_Sorting" 
             BorderStyle="Ridge" BorderWidth="2px"  HorizontalAlign="Center"
             GridLines="Both" ShowHeader="True" ShowFooter="False" >
            <HeaderStyle BackColor="#4A3C8C" ForeColor="#E7E7FF"></HeaderStyle>
                <Columns>
                    <asp:TemplateField HeaderText="Station"  SortExpression="number">
                    <ItemTemplate>
                        <asp:CheckBox ID="Station" Text='<%#Bind("number")%>' runat="server"/>
                    </ItemTemplate>
                        <ItemStyle HorizontalAlign="Center" Width="130px"></ItemStyle>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Server" >
                    <ItemTemplate>
                        <asp:DropDownList DataTextField="Server" DataValueField="Server" ID="DropDownList2" Runat="server" >
                            <asp:ListItem>A1</asp:ListItem>
                            <asp:ListItem>A2</asp:ListItem>
                            <asp:ListItem>Both</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                        <ItemStyle HorizontalAlign="Center" Width="130px"></ItemStyle>
                    </asp:TemplateField>

                    <asp:BoundField DataField="TimeZone" HeaderText="Time Zone" SortExpression="timeZone">
                        <ItemStyle HorizontalAlign="Center" Width="130px" />
                    </asp:BoundField>

                    <asp:BoundField DataField="ServerType" HeaderText="Server Type" SortExpression="serverType">
                        <ItemStyle HorizontalAlign="Center" Width="135px" />
                    </asp:BoundField>               
                </Columns>
                <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
             </asp:GridView>                   
        </div> 
        </td></tr> 
        </ItemTemplate>
        <HeaderStyle Width="305px"></HeaderStyle>
        <ItemStyle HorizontalAlign="Center" Width="280px"/>
        </asp:TemplateField>                                    
    </Columns>
<BoundaryStyle BorderColor="Gray" BorderWidth="1px" BorderStyle="Solid"></BoundaryStyle>
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
     </cc1:CoolGridView>

<div style="float: right; width: 27%; height: 234px; margin-left: 0px;" 
         id="RightColumn" align="center" dir="ltr"> 
    <p style="height: 25px; width: 230px; margin-left: 5px;"> </p>

    <asp:Button ID="BtnCreateXMLFile" runat="server" Height="51px" 
        style="margin-bottom: 0px; text-align: center; margin-left: 10px;" 
        Text="Create XML File" Width="185px" onclick="BtnCreateXMLFile_Click"/> 

    <p style="height: 24px; width: 226px; margin-left: 5px;"> </p>

    <asp:Button ID="BtnClearSelections" runat="server" Height="35px"
        style="margin-bottom: 0px; text-align: center; margin-left: 10px"
        Text="Clear Selections" Width="150px" 
        onclick="BtnClearSelections_Click"/>   
</div>               
</form>

1 个答案:

答案 0 :(得分:0)

好吧,对于初学者来说,第一行的大部分都在第二行中重复,因此您可以通过将临时变量设置为重复位来删除所有重复的代码(从而重复工作):

var $found = chkStation.parent().parent().parent().parent().parent().closest("tr").siblings().find('td:contains(' + districtName + ')');

$found.attr('style','background-color:lime');  
$found.siblings().attr('style','background-color:lime');

这是一个直接的大豁免,而不会改变任何实际逻辑。

接下来,我们可以减少所有parent()次来电。事实上,我们可能完全摆脱它们,因为closest()无论如何都会爬上树来找到tr元素。

您尚未向我们展示适用的HTML,但假设它不包含嵌套表格,那么您可以完全放弃所有parent()次调用。

(如果它确实包含嵌套表,那么你仍然可以减少很多,但我需要看到HTML来提供准确的解决方案)

最后,您的代码似乎是设置一行中所有td元素的背景颜色。如果只是在tr上设置它,您将获得更好的性能。事实上,这甚至可能意味着你可以在不需要两行代码的情况下离开。

可能还有更多可以做的事情;看到你的HTML可能对我有所帮助,但我会停在这里,因为我已经覆盖了很多。

我希望能给你一些思考的食物。

相关问题