jQuery如何动态匹配href中f为任意数字的特定链接?
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 tof)[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

