簡體   English   中英

即使div中的單個ul li為空也要刪除div

[英]remove div even if single ul li empty inside the div

我有以下代碼。

<div id="rem-download-ref" class="download-reference">
    <ul>
        <li><strong>Download »</strong></li>
        <li><asp:Image ID="imgepub" runat="server" align="absmiddle" ImageUrl="/images/icon/epub.png" Visible="false"  width="22" height="22" /><asp:HyperLink ID="HyperLinkePub" runat="server"
                 NavigateUrl='<%# Eval("epub_path").ToString() + Eval("epub_name").ToString() %>'
                 Visible="false">ePUB</asp:HyperLink>
        </li>
        <li><asp:Image ID="imgmobi" runat="server" align="absmiddle" ImageUrl="/images/icon/mobi.png" Visible="false"  width="22" height="22" /><asp:HyperLink ID="HyperLinkMobi" runat="server"
                 NavigateUrl='<%# Eval("mobi_path").ToString() + Eval("mobi_name").ToString() %>'
                 Visible="false">mobi</asp:HyperLink>
         </li>
    </ul>
</div>

我需要的是如果任何<li>為空,那么我想刪除整個div。

如何使用javascript做到這一點?

您的要求不太明確。 但這就是你想要的嗎?

$("#rem-download-ref li").each(function(i, v) {
    if ($(v).text() == "") {
        $("#rem-download-ref").remove();
        break;
    }
});

請不要忘記添加jQuery

<script type="text/javascript" src="http://jquery.com/src/jquery-latest.js"></script>

將您的<div><asp:Panel> 然后,在容器的數據綁定事件上,如果epub_pathmobi_path為null或為空,則將面板的可見性設置為false。

如果沒有更多代碼,很難給出更詳細的答案。 哪種控件正在執行數據綁定? GridView ListView Repeater 數據源是什么類型的對象?

看看是否有幫助,您可能需要將其包裝到一個函數中或至少放置在rem-download-ref

確保所有元素內聯沒有空格

<div id="rem-download-ref" class="download-reference"><ul><li id="o1"><strong id="ccc">Download »</strong></li><li id="o2"></li></ul></div>

var dc = document.getElementById("rem-download-ref").firstChild

for (var n = 0; n <dc.childNodes.length; n++) {
    //if (rem-download-ref not null and li not null remove rem-download-ref
   if(dc.parentNode && dc.childNodes[n].firstChild==null){
  dc.parentNode.removeChild(dc);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM