我在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>·</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>
答案 0 :(得分:-1)
您有2个主要问题。首先,对于没有扩展名的文件,您的扩展程序检查将失败。检查一下:
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");
}
});