1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

javascriptのattr()ってなに?

Posted at

attr()はHTML要素の属性を取得、設定することができるメソッドです。

以下のようなコードがあるとしましょう。

    <a href="https://example.com" id="example-link">Go to Example</a>
  • 属性の取得
var hrefValue = $('#example-link').attr('href');
console.log(hrefValue); // コンソールに 'https://example.com' と表示されます

idがexample-linkである要素のhref属性を取得できます。

  • 属性の設定
hrefValue = $('#example-link').attr('href', 'https://newexample.com');
console.log(hrefValue); // コンソールに 'https://newexample.com' と表示されます

attrメソッドの第2引数の値を設定します。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?