1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

「attr()」と「prop()」の違いは?

1
Posted at

samuraiの「30分で理解!jQueryのattr()で属性操作を極めるコツ!
」を引用したものです。
https://www.sejuku.net/blog/37402

「attr()」と「prop()」の共通項は?

attrもpropも属性を取得するものです。

「attr()」と「prop()」の違いは?

<body>
<button class= "btn1">ボタン1</button>
<button class= "btn2" disabled>ボタン2</button>

<script>
var result1 = $('.btn1').attr('disabled');
var result2 = $('.btn2').attr('disabled');

var result3 = $('.btn1').prop('disabled');
var result4 = $('.btn2').prop('disabled');

console.log(result1);
console.log(result2);
console.log(result3);
console.log(result4);
</script>
</body>

=>undefined
=>disabled
=>false
=>true

「attr()」では、「undefined」になる
→属性が付与されているかどうか
「prop()」では、falseになる
→現在の状態の確認

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?