如何移除导航中指定分类标签与标题的href属性?
Hey there! I see you're trying to remove href attributes only from the First and Second Category's tab and header links, while leaving all others intact. Your original selector was too broad—it was targeting every top-level a tag in the navigation, which is why all links lost their href. Let's narrow this down precisely.
The Exact jQuery Script You Need
We'll target only the links inside the .FirstCat and .SecondCat list items, specifically:
- The direct child
a(the category tab) - The
ainside the dropdown'sh3(the category header)
Here's the script:
// Remove href from First & Second Category tabs and headers $(".FirstCat > a, .FirstCat .dropdown h3 > a, .SecondCat > a, .SecondCat .dropdown h3 > a").removeAttr("href");
A Cleaner Alternative
If you prefer more concise code, you can first target the parent list items, then find the specific links within them:
// Target parent categories first, then select the links inside $(".FirstCat, .SecondCat").find("> a, .dropdown h3 > a").removeAttr("href");
Why This Works
.FirstCat > aselects only the direct childatag (the "First Category Tab")—it won't touch links deeper in the dropdown..FirstCat .dropdown h3 > atargets exactly the header link inside the First Category's dropdown.- We repeat this pattern for
.SecondCatto cover its tab and header. - All other links (Third/Fourth Category tabs/headers, plus product/service links) remain untouched, keeping their
hrefattributes as intended.
Test this out, and you'll see only the four links you specified lose their href while everything else stays linked!
内容的提问来源于stack exchange,提问作者Gozer516

