如果 a 链接的 href 中包含某些内容,请将 href 更改为此

发布于 2024-10-05 23:40:21 字数 155 浏览 1 评论 0原文

我想检查 href 是否包含“即将推出”,如果它确实更改了 href 以转到我的产品页面:

$('a[href$="coming-soon"]').attr('href', '/products.aspx');

无法解决此问题。

i want to check if the href contains 'coming-soon' if it does change the href to go to my products page:

$('a[href$="coming-soon"]').attr('href', '/products.aspx');

cant work this out.

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

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

发布评论

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

评论(4

甜嗑 2024-10-12 23:40:21

$= 是“attribute-ends-with”,使用 *= 表示“属性包含”,像这样:

$('a[href*="coming-soon"]').attr('href', '/products.aspx');

$= is "attribute-ends-with", use *= for "attribute contains", like this:

$('a[href*="coming-soon"]').attr('href', '/products.aspx');
黑白记忆 2024-10-12 23:40:21

使用包含选择器

$(document).ready(function(){
   $("a[href*='coming-soon']").attr('href', '/products.aspx');
});

在这里尝试一下!

Use the contains selector.

$(document).ready(function(){
   $("a[href*='coming-soon']").attr('href', '/products.aspx');
});

Try it here!

一梦浮鱼 2024-10-12 23:40:21

$= 表示“以”结尾。您想查找即将推出的包含(URL 中任意位置)的链接吗?尝试使用 *= 而不是 $=

$= means "ends with". You want to find links that contain (anywhere in the URL) coming-soon? Try *= instead of $=.

手心的温暖 2024-10-12 23:40:21

您的选择器当前是 href coming-soon 结尾的任何锚标记。包含选择器是 *=

$('a[href*="coming-soon"]').attr('href', '/products.aspx');

请参阅 http://api.jquery .com/attribute-contains-selector/

如果这不是问题,请确保加载 DOM 时运行代码;将代码包装在 $(document).ready()

$(document).ready(function () {
    $('a[href*="coming-soon"]').attr('href', '/products.aspx');
});

Your selector is currently any anchor tag whose href ends with coming-soon. The contains selector is *=:

$('a[href*="coming-soon"]').attr('href', '/products.aspx');

See http://api.jquery.com/attribute-contains-selector/

If this isn't the problem, ensure the code is ran when the DOM is loaded; by wrapping the code in the $(document).ready()

$(document).ready(function () {
    $('a[href*="coming-soon"]').attr('href', '/products.aspx');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文