iterating with each and contains in jquery
Given this html markup
<tr>
<td class="tdDescCell"><b><i>Product Savings: </i></b></td>
<td class="tdDescCell"><b><i> <asp:Label ID="lblEBSavings_Q1" Text="" cssClass="labelValueCalc" runat="server" ClientIDMode="Static"></asp:Label></i></b></td>
<td class="tdDescCell"><b><i> <asp:Label ID="lblEBSavings_Q2" Text="" cssClass="labelValueCalc" runat="server" ClientIDMode="Static"></asp:Label></i></b></td>
<td class="tdDescCell"><b><i> <asp:Label ID="lblEBSavings_Q3" Text="" cssClass="labelValueCalc" runat="server" ClientIDMode="Static"></asp:Label></i></b></td>
<td class="tdDescCell"><b><i> <asp:Label ID="lblEBSavings_Q4" Text="" cssClass="labelValueCalc" runat="server" ClientIDMode="Static"></asp:Label></i></b></td>
</tr>
Which renders like
<tr>
<td class="tdDescCell"><b><i>Product Savings: </i></b></td>
<td class="tdDescCell"><b><i> <span id="lblEBSavings_Q1" class="labelValueCalc"></span></i></b></td>
<td class="tdDescCell"><b><i> <span id="lblEBSavings_Q2" class="labelValueCalc"></span></i></b></td>
<td class="tdDescCell"><b><i> <span id="lblEBSavings_Q3" class="labelValueCalc"></span></i></b></td>
<td class="tdDescCell"><b><i> <span id="lblEBSavings_Q4" class="labelValueCalc"></span></i></b></td>
</tr>
I use the following jquery to set the labels text, which seems quite redundant and sloppy.
$("#lblEBSavings_Q1").text("$" + ebSavingsQtrly.toString());
$("#lblEBSavings_Q2").text("$" + ebSavingsQtrly.toString());
$("#lblEBSavings_Q3").text("$" + ebSavingsQtrly.toString());
$("#lblEBSavings_Q4").text("$" + ebSavingsQtrly.toString());
I tried to refactor using each and contains like the following line,
$("#fld_ROICalcOutput > span").contains("lblEBSavings").each().text("$" + ebSavingsQtrly.toString());
Where #fld_ROICalcOutput is the parent fieldset element of the table. Where am I going wrong here and is there a better way to express what I am trying to accomplish here.
我认为你的目标是带有lblEBSavings
类的span
元素,在这种情况下,不需要使用.each()
$('.labelValueCalc[id^="lblEBSavings"]').text("$" + ebSavingsQtrly.toString())
Try using the each function as following:
$.each($("#fld_ROICalcOutput > span").contains("lblEBSavings"), function(){
$(this).text("$" + ebSavingsQtrly.toString());
});
You should also try to console.log( $("#fld_ROICalcOutput > span").contains("lblEBSavings") );
to make sure that the selector works.
怎么样:
$("#fld_ROICalcOutput[id^=lblEBSavings]").each(function() {
$(this).text("$" + ebSavingsQtrly.toString());
}
链接地址: http://www.djcxy.com/p/74106.html
上一篇: 大写标题页如果
下一篇: 迭代每个并包含在jquery中