我有这个按我想要的方式工作的 jquery 代码。It highlights the color of a row when the child is selected. 但是,它很慢。这是代码行:
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>