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

如何通过元素的唯一路径值增删对应a标签的href属性

实现方案

分原生JavaScript和jQuery两种常用实现方式,操作前需注意要提前缓存原始href值,避免移除属性后无法获取原始值。

原生JavaScript实现

移除href属性

// 匹配目标a标签
const targetLink = document.querySelector('a[href="Edit/44"]');
// 缓存原始href值供后续恢复使用
const originalHref = targetLink.getAttribute('href');
// 移除href属性
targetLink.removeAttribute('href');

恢复href属性

// 确认缓存值存在后赋值
if (originalHref) {
  targetLink.setAttribute('href', originalHref);
}

jQuery实现

移除href属性

// 缓存原始href值
const originalHref = $('a[href="Edit/44"]').attr('href');
// 移除href属性
$('a[href="Edit/44"]').removeAttr('href');

恢复href属性

$('a[href="Edit/44"]').attr('href', originalHref);

注意:上述选择器为精确匹配href属性值,如果实际页面渲染后a标签的href值包含前缀(比如/Edit/44、完整域名路径等),需要把选择器中href的匹配值修改为实际渲染的值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 10:24:03