javascript - 如何使用 Javascript 访问严格/显式 href 值?
问题描述
给定以下锚点:
<a class="example" href="record1234">link</a>
如何href
使用 vanilla Javascript 检索实际值?
const link = document.querySelector('.example');
console.log(link.href);
// https://exampledomain.com/current/document/path/record1234
console.log(link.pathname)
// /current/document/path/record1234
// with jQuery
$('.example').attr('href');
// what I want to do:
console.log(link.href.string); // record1234
解决方案
.getAttribute
在元素上
使用。
const link = document.querySelector('.example');
console.log(link.getAttribute('href'));
<a class="example" href="record1234">link</a>
推荐阅读
- flutter - Flutter-路由到不同的 .dart 文件
- python - PyQt5 PlotDataItem 用于单图中的多个实时流
- ruby-on-rails - 如何找到具有特定关联值的所有记录?
- java - JavaFX ListChangeListener 多线程
- python - 使用 format() 将默认值设置为 NULL 返回意外的字符串
- python - 使用 `matplotlib` 和 `pandas` 表示连续图
- go - 忽略“已导入且未使用”的编译时错误
- sdk - 在 sdk 中由 Convertquotetosalesorder 创建的 Salesorder 的状态码
- javascript - 在 PHP 中使用循环将 div ID 存储到数组中
- java - 当用户定义变量或函数的类型时,Java CUP(解析器)会产生移位/减少冲突