You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

jQuery如何动态匹配href中f为任意数字的特定链接?

Solution for Dynamic f Parameter in href Selector

Got it, let's solve this problem where you need to target all "new topic" links regardless of the dynamic f parameter value in their href.

The Issue with Your Current Selector

Your current selector $('a[href="/post?f=1&mode=newtopic"]') uses an exact match for the entire href string, which is why it only works when f=1. We need a flexible way to match any value for f while keeping the rest of the link pattern consistent.

The Fix: Use jQuery's Partial Attribute Selectors

We can combine two partial attribute selectors to target the right links reliably:

  • [href^="/post?f="]: Matches links where the href starts with /post?f= (this covers any numeric or alphanumeric value assigned to f)
  • [href*="&mode=newtopic"]: Matches links where the href contains &mode=newtopic (ensures we only target links for creating new topics, not other post-related actions)

Putting these together gives us a selector that works for any f parameter value.

Modified Code

$(document).ready(function() {

    var total_posts = _userdata["user_posts"];
    var minimum_posts = 5;

    if (total_posts < minimum_posts) {
        // Target all new topic links with any f parameter value
        $('a[href^="/post?f="][href*="&mode=newtopic"]').css("pointer-events", "none")
    }

});

Extra: Handling Edge Cases (Optional)

If there's a rare chance the href might have mode=newtopic without an ampersand (e.g., if mode is the only parameter after f), you can adjust the second selector to cover both scenarios:

$('a[href^="/post?f="][href*="mode=newtopic"]')

This will match both &mode=newtopic and ?mode=newtopic—though in your case, since f is the first parameter, the ampersand should always be present, but this adds a safe fallback.

内容的提问来源于stack exchange,提问作者Aleksandar Arsic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 17:36:10