LoginSignup
6
3

More than 5 years have passed since last update.

.getAttribute('href')と.hrefの違い

Last updated at Posted at 2018-02-02

a要素に対し element.getAttribute('href')すると属性値がそのまま、element.hrefには絶対URIが入る。

HTML
<a href="../test.html" id="link">link</a>
JavaScript
var link = document.getElementById('link');
console.log("link.getAttribute('href') --> " + link.getAttribute('href'));
console.log("link.href                 --> " + link.href);
CODEPEN上での実行結果
link.getAttribute('href') --> ../test.html
link.href                 --> http://s.codepen.io/boomerang/test.html

CODEPENで見る

参考サイト

href of type DOMString
The absolute URI [IETF RFC 2396] of the linked resource.
6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3