๐ก attr() VS prop() ๐ก
jQuery 1.6.0 ์ ๋ฐ์ดํธ๋ก attr() ๊ณผ prop() ๋ ๊ฐ๋ก ๋๋ ์ก๋ค.
- attr() : HTML ์์ฑ (Attribute) ์ทจ๊ธ
- prop() : javascript ํ๋กํผํฐ (Property) ์ทจ๊ธ
- * ์์ฑ(Attribute) ์ HTML ์์์ ๋ํ ์ถ๊ฐ ์ ๋ณด๋ฅผ ์ ๋ฌํ๋ฉฐ ์์ผ๋ก ์ ๊ณต
- * ํ๋กํผํฐ(Property) ๋ HTML DOMํธ๋ฆฌ์ ํน์ฑ์ผ๋ก javaSctipt / jQuery๋ฅผ ํตํด ์์ ๋ ์์์ ๊ฐ์ ๊ฐ์ ธ์ค๋๋ฐ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์
// .html
<input type="checkbox" id="example" checked="checked">
// .js
// attr()
var text = $("input[type=text]");
console.log(chk.attr("id")); // example
console.log(chk.attr("type")); // text
console.log(chk.attr("checked")); // checked
// .js
// prop()
var text = $("input[type=text]");
console.log(chk.prop("id")); // example
console.log(chk.prop("type")); // text
console.log(chk.prop("checked")); // true
์์ ์์ ๋ฅผ ๋ณด๋ฉด id์ type ๊ฐ์ ๋์ผํ๋ checked์์ ๋ค๋ฅธ ๊ฐ์ ํ์ธ ํ ์ ์๋ค.
attr() ์ HTML ์์ฑ ๊ฐ์ด ๋ชจ๋ String์ ํ์ ์ด์ง๋ง prop() ๋ boolean, date, function ๋ฑ์ผ๋ก ๊ฐ์ ธ์ฌ ์ ์๋ค.
attr() / prop() ๋ ์ค ๋ฌด์์ด ์ข์๊น?
๊ฐ ์ฌ์ฉ์๋ค์ด ์ฌ์ฉํ๋ ๋ชฉ์ ์ ๋ฐ๋ผ ๋ค๋ฅผ ์ ์์ง๋ง prop() ๊ฐ attr() ๋ณด๋ค ์ฝ 2.5๋ฐฐ ๋น ๋ฅด๋ค๋ ์ฅ์ ์ ๊ฐ์ง๋ค.
HTML์ ์์ฑ์ ๊ฐ์ง๊ณ ์ค๊ณ ์ถ์ ๋๋ attr() ์,
javaScript๋ก ์์ ๋ ์์์ ๊ฐ์ ๊ฐ์ง๊ณ ์ถ์ ๋๋ prop() ์ฌ์ฉ์ ๊ถํ๋ค.
์ฐธ๊ณ
[jQuery] attr()๊ณผ prop()์ ์ฐจ์ด์ ์ ๋ฌด์์ผ๊น?
๐ก ์ค๋์ attr()๊ณผ prop()์ ์ฐจ์ด์ ์ ์์๋ณด๊ฒ ์ต๋๋ค. attr() VS prop() jQuery 1.6.0 ์ ๋ฐ์ดํธ๋ก attr() ๊ณผ prop() ๋ ๊ฐ๋ก ๋๋ ์ก๋ค. - attr() : HTML ์์ฑ (Attribute) ์ทจ๊ธ - prop() : javascript ํ๋กํผํฐ (Property) ์ทจ๊ธ *
yeonzzy.tistory.com
'jQuery > ๊ธฐ๋ณธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[jQuery] html(), text(), val() ๊ตฌ๋ถ (0) | 2023.03.17 |
---|---|
[jQuery] ํน์ ์์ญ ์ธ ํด๋ฆญ ์ด๋ฒคํธ e.target / e.currentTarget (0) | 2023.03.16 |
input form reset function (0) | 2022.10.17 |
4. ๊ฐ์ฒด ์กฐ์ - ์์ (0) | 2022.06.09 |
4. ๊ฐ์ฒด ์กฐ์ - ๊ฐ์ฒด ํธ์ง ๋ฉ์๋ (0) | 2022.06.09 |