Javascript 使用 jQuery 在新窗口中打开任何带有 .pdf 的链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14140446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:49:24  来源:igfitidea点击:

Make any link with .pdf open in new window with jQuery?

javascriptjqueryhyperlink

提问by Digi Jeff

How can I have all links with a .pdffile extension open in a new window using jQuery? I need to change this:

如何.pdf使用 jQuery 在新窗口中打开所有带有文件扩展名的链接?我需要改变这个:

<a href="domain.com/pdf/parkingmap.pdf">parking map</a>

In to this:

对此:

<a href="domain.com/pdf/parkingmap.pdf" target="_blank">parking map</a>

All files are in a /pdffolder if that helps.

/pdf如果有帮助,所有文件都在一个文件夹中。

回答by Rory McCrossan

To achieve this you can select any aelement which has a hrefproperty ending with .pdf, and add a target="_blank"attribute to it. Try this:

为此,您可以选择a具有以href结尾的属性的任何元素.pdf,并target="_blank"为其添加属性。尝试这个:

$(function() {
    $('a[href$=".pdf"]').prop('target', '_blank');
});

回答by Hiren Patel

<a onclick=ViewPdf(test.pdf) href="">


function ViewPdf(FileName) {
    var url = '../Home/GetPDF?fileName=' + FileName;
    window.open(url, '_blank');

}

Now write ActionResult like below

现在像下面这样写 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");
            }
        }

回答by David says reinstate Monica

One way, assuming you want links notending in pdfto open in the same page:

一种方法,假设您希望链接以在pdf同一页面中打开为结尾:

$('a').click(
    function(e){
        e.preventDefault();
        if (this.href.split('.').pop() === 'pdf') {
            window.open(this.href);
        }
        else {
            window.location = this.href;
        }
    });

回答by gabssnake

jQuery one-liner :

jQuery 单线:

$('a[href$=".pdf"]').attr('target','_blank');

Also in vanilla Javascript :

同样在香草 Javascript 中:

[].filter.call(document.querySelectorAll('a'), function(a){
    return a.href.match('\.pdf$') ? a.target = '_blank' : 0;
});

Or maybe :

或者可能 :

var anchors = document.body.getElementsByTagName('a');
for (var i = 0; i < anchors.length; i++) {
    if(anchors[i].getAttribute('href').match('\.pdf$') {
        anchors[i].setAttribute('target', '_blank');
    }
}

Try it here : http://codepen.io/gabssnake/pen/KyJxp

在这里试试:http: //codepen.io/gabssnake/pen/KyJxp