0

我在 asp.net 中继器中有下载链接以下载 .pdf 文件,但我想隐藏没有 .pdf 文件扩展名的链接。
javascript

 // get file extensions
    var fileURL = $('hl_download').attr("href");
    // get file extensions
    var fileURL = $('hl_download').attr("href");


    function getFileExtension(fileURL) {
        return filename.split('.').pop();
    }


    var fileExtension =  getFileExtension(fileURL);
    if (fileExtension == null) {
        $('hl_download').addClass("hideLinke");
    }

    asp-markup
        <asp:Repeater ID="rp_resList" runat="server" OnItemDataBound="rp_resList_ItemDataBound" OnItemCommand="rp_resList_ItemCommand">
                                <ItemTemplate>
                                    <div class="resource">
                                        <div class="resource-head clearfix">
                                            <img class="pull-left" src="/ideapark/DesktopModules/ResourcesFilter/img/<%#  Eval("typeId") %>.png" alt="icon type" width="36" height="36" />
                                            <p class="resource-title pull-left">
                                                <asp:Literal ID="litType" runat="server" Text='<%# Eval("resType")  %>'></asp:Literal>
                                            </p>
                                            <div class="favorite-resource favorited pull-right">
                                                <asp:ImageButton ID="imgBtn_bookmark" runat="server" CommandName="bookmark_res" ImageUrl="~/DesktopModules/ResourcesFilter/img/off.png" />
                                            </div>
                                        </div>
                                        <div class="resource-body">
                                            <asp:HiddenField ID="hf_resID" runat="server" Value='<%#  Eval("Id") %>' />
                                            <p class="resource-subtitle"><a href='<%#  Eval("pageID") %>'><%#  Eval("name") %></a></p>
                                            <p>
                                                <asp:Literal ID="litSummary" runat="server" Text='<%#  Eval("summary") %>'></asp:Literal>
                                            </p>
                                            <div class="resource-links">
                                                <p><a class="resource-like" href="#">Like</a> <strong>&middot;</strong> <asp:HyperLink ID="hl_download" runat="server" CssClass="resource-download" NavigateUrl='<%# Eval("pdf_Doc_url") %>' ClientIDMode="Static">Download</asp:HyperLink>  </p>
                                            </div>
                                            <div class="resource-tags clearfix">
                                                <asp:Repeater ID="rp_tagsTopics" runat="server">
                                                    <ItemTemplate>
                                                        <span class="resource-tag pull-left">
                                                            <asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%#  Container.DataItem %>' OnClick="LinkButton1_Click"> <%#  Container.DataItem %></asp:LinkButton>
                                                        </span>
                                                    </ItemTemplate>
                                                </asp:Repeater>
                                            </div>
                                        </div>
                                    </div>

                                </ItemTemplate>
                            </asp:Repeater>
4

1 回答 1

-1

您有两个主要问题。首先,对于没有扩展名的文件,您的扩展名检查将失败。检查这个:

function getFileExtension(fileURL) {
  if(filename.indexOf('.') === -1){return "";} //check for no extension
  return filename.split('.').pop();
}

其次,您没有检查任何特定的链接。使用 JQuery 查找所有这些,并一次隐藏它们。假设您所指的链接是“类似资源的”:

$.each($('.resource-like'),function(){
  var fileExtension =  getFileExtension(this.href);
  if (fileExtension == "" || fileExtension != "pdf") {
     $(this).addClass("hideLinke");
  }
});
于 2013-07-29T14:50:43.397 回答