使用 jQuery 在新窗口中打开任何带有 .pdf 的链接? [英] Make any link with .pdf open in new window with jQuery?

查看:19
本文介绍了使用 jQuery 在新窗口中打开任何带有 .pdf 的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

对此:

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

如果有帮助,所有文件都在 /pdf 文件夹中.

All files are in a /pdf folder if that helps.

推荐答案

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

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

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

这篇关于使用 jQuery 在新窗口中打开任何带有 .pdf 的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆