如何使用 jQuery 查找具有特定扩展名的链接

发布于 2024-09-30 10:16:07 字数 285 浏览 2 评论 0原文

我正在尝试遍历并查找具有特定扩展名 (*.ashx) 的链接,以便我可以在新选项卡中打开该链接。 (Sitefinity 不允许 target="_blank")。

我可以使用 jQuery 找到标签,但我需要对其进行更多过滤,以便当我单击扩展名为 .ashx 的标签时,我可以在新窗口中打开它。

像这样的事情

<a href="anniversary.sflb.ashx"> Anniversary </a> 

非常感谢, 詹姆斯

I am trying to traverse and find links with a specific extension (*.ashx) so that I can open the link in a new tab. (Sitefinity does not allow target="_blank").

I can find the tags using jQuery, but I need to then filter it more so that when I click on an tag with an extension of .ashx, I can open this in a new window.

Something like this

<a href="anniversary.sflb.ashx"> Anniversary </a> 

Many thanks,
James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦境 2024-10-07 10:16:07

最好使用属性包含选择器,

$("a[href*='.ashx']").each(
  function() {
  //Do stuff
});

请参阅 http://jsfiddle.net/graham/Fa6kV/< /a>

It would be better to use the attribute contains selector

$("a[href*='.ashx']").each(
  function() {
  //Do stuff
});

see http://jsfiddle.net/graham/Fa6kV/

病女 2024-10-07 10:16:07

到目前为止提供的两个答案(graham 和 Steve)都可能不准确:graham 有时会匹配字符串中某处有 ashx 的链接,但不一定在字符串末尾 - 例如,bashxml.php 会匹配。与此同时,史蒂夫指出,如果存在查询字符串,他的将不会匹配。

您可以通过自己进行过滤来解决这个问题:

$('a').each(function() {
    if (this.pathname.substr(-5) === '.ashx') { // if the last 5 characters of the pathname are .ashx
        // do your processing here
    }
});

请参阅有关 Location 对象的文档。

The two answers provided so far (graham's and Steve's) are both potentially inaccurate: graham's will sometimes match links that have ashx somewhere in the string, but not necessarily at the end of the string -- bashxml.php would match, for instance. Steve's, meanwhile, will not match if there's a query string, as he notes.

You can get round this by doing the filtering yourself:

$('a').each(function() {
    if (this.pathname.substr(-5) === '.ashx') { // if the last 5 characters of the pathname are .ashx
        // do your processing here
    }
});

See documentation on the Location object.

锦欢 2024-10-07 10:16:07

您可以使用属性以选择器结尾

$("a[href$='.ashx']")

注意:这不起作用如果您的链接的 href 属性中有查询字符串或其他后缀

You could use the attribute ends with selector:

$("a[href$='.ashx']")

Note: this wouldn't work if your links have a query string or some other suffix in the href attribute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文