【问题标题】:How to access strict/explicit href value with Javascript? [duplicate]如何使用 Javascript 访问严格/显式 href 值? [复制]
【发布时间】:2021-02-05 16:55:40
【问题描述】:

给定以下锚点:

<a class="example" href="record1234">link</a>

如何使用 vanilla Javascript 检索 href 的实际值?

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

【问题讨论】:

    标签: javascript


    【解决方案1】:

    在元素上使用.getAttribute

    const link = document.querySelector('.example');
    console.log(link.getAttribute('href'));
    <a class="example" href="record1234">link</a>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-13
      • 2018-07-19
      • 2012-11-09
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多