在我的 Dom 中,我得到了一些li
,其中包括一些 as.net 文本框作为它们的子项。我需要将一些文本框设置为可见 false。当我这样做时,li
围绕此文本框的包裹在 html 中显示为空.所以我决定删除它们,我使用了这段代码(还有更多)
<script type="text/javascript">
$(document).ready(function (e) {
$('#u li').each(function () {
if ($(this).html().length == 0) {
$(this).remove();
}
});
});
</script>
令我惊讶的是,没有li
进入空的循环。注意添加的 HTML
<ul id="u">
<li class="first-item odd-item">
<span id="ContentPlaceHolderDefault_cphContents_ShopConfirmation_2_lblFirstname">Firstname</span>
<input type="text" id="ContentPlaceHolderDefault_cphContents_ShopConfirmation_2_txtFirstname" name="ctl00$ctl00$ctl00$ContentPlaceHolderDefault$cphContents$ShopConfirmation_2$txtFirstname">
<span style="color:Red;visibility:hidden;" id="ContentPlaceHolderDefault_cphContents_ShopConfirmation_2_reqirefieldFirstname">*</span>
</li>
<li class="odd-item">
<span class="m-left" id="ContentPlaceHolderDefault_cphContents_ShopConfirmation_2_lblLastname">Lastname</span>
<input type="text" id="ContentPlaceHolderDefault_cphContents_ShopConfirmation_2_txtLastname" name="ctl00$ctl00$ctl00$ContentPlaceHolderDefault$cphContents$ShopConfirmation_2$txtLastname">
<span style="color:Red;visibility:hidden;" id="ContentPlaceHolderDefault_cphContents_ShopConfirmation_2_reqirefieldLastname">*</span>
</li>
<li class="third-item odd-item">
<span id="ContentPlaceHolderDefault_cphContents_ShopConfirmation_2_lblEmployeenumber">Emloyee Number</span>
<input type="text" id="ContentPlaceHolderDefault_cphContents_ShopConfirmation_2_txtEmployeenumber" name="ctl00$ctl00$ctl00$ContentPlaceHolderDefault$cphContents$ShopConfirmation_2$txtEmployeenumber">
</li>
<li class="odd-item">
</li>
<li class="odd-item">
<span id="ContentPlaceHolderDefault_cphContents_ShopConfirmation_2_lblAddress">Address</span>
<select id="ContentPlaceHolderDefault_cphContents_ShopConfirmation_2_dropdownaddress" name="ctl00$ctl00$ctl00$ContentPlaceHolderDefault$cphContents$ShopConfirmation_2$dropdownaddress">
<option value="-----------Select-----------">-----------Select-----------</option>
<option value="dfddfd">dfddfd</option>
<option value="gdfgfg">gdfgfg</option>
<option value="fhgfgfgf">fhgfgfgf</option>
</select>
</li>
<li class="third-item last-item odd-item">
</li>
</ul>