如何使用jQuery在新窗口中打开带有.pdf文件扩展名的所有链接?我需要改变这一点:
<a href="domain.com/pdf/parkingmap.pdf">parking map</a>在这方面:
<a href="domain.com/pdf/parkingmap.pdf" target="_blank">parking map</a>如果有帮助,所有文件都在/pdf文件夹中。
发布于 2013-01-03 22:06:39
为此,您可以选择任何具有以.pdf结尾的href属性的a元素,并向其添加一个target="_blank"属性。试试这个:
$(function() {
$('a[href$=".pdf"]').prop('target', '_blank');
});发布于 2013-01-03 22:08:18
一种方法是,假设您希望在同一页面中打开不以pdf结尾的链接:
$('a').click(
function(e){
e.preventDefault();
if (this.href.split('.').pop() === 'pdf') {
window.open(this.href);
}
else {
window.location = this.href;
}
});发布于 2019-05-22 22:51:57
<a onclick=ViewPdf(test.pdf) href="">
function ViewPdf(FileName) {
var url = '../Home/GetPDF?fileName=' + FileName;
window.open(url, '_blank');
}
现在像下面这样编写ActionResult
public ActionResult GetPDF(string fileName)
{
try
{
byte[] fileData = System.IO.File.ReadAllBytes(Functions.GetConfigValue("CDXFilePath") + fileName);
string resultFileName = String.Format("{0}.pdf", fileName);
Response.AppendHeader("Content-Disposition", "inline; filename=" + resultFileName);
return File(fileData, "application/pdf");
}
catch
{
return File(Server.MapPath("/Content/") + "FileNotFound.html", "text/html");
}
}https://stackoverflow.com/questions/14140446
复制相似问题