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

jQuery如何选取所有a标签href属性并排除main元素内的链接

jQuery排除main内部a标签获取href实现方案

核心修改逻辑

仅需将原选择器修改为排除main标签后代的a标签即可,有两种符合要求的实现方式:

写法1:选择器内置过滤

$('a:not(main a)').each(function() {
  console.log($(this).attr('href'));
});

写法2:调用not方法过滤

$('a').not('main a').each(function() {
  console.log($(this).attr('href'));
});

效果说明

上述两种写法均仅使用了main相关选择器实现过滤,运行后只会输出header和footer中a标签的href值url1、url3,不会读取main内部的url2,完全符合需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 08:30:05