반복되는 DOM요소를 커스텀 태그로 만들 수 있다
<script>
customElements.define('custom-input', Cls);
<script>
** class Cls extends HTMLElement {
connectedCallback() {
this.innerHTML = `<label>라벨명</label><input>`;
}
}**
customElements.define('custom-input', Cls);
<body>
<custom-input></custom-input>
<script>
class Cls extends HTMLElement {
connectedCallback() {
let name = this.getAttribute('name')
this.innerHTML = `<label>${name}</label><input>`;
}
<script>
class Cls extends HTMLElement {
connectedCallback() {
let name = this.getAttribute('name')
this.innerHTML = `<label>${name}</label><input>`;
}
// attr 변경 감지 가능. name 속성 변경됨을 감지한다.
static get observedAttributes() {
return ['name']
}
// 변경 감지 시 실행할 코드
attributeChangedCallback() {
console.log('sdf')
}
}
<body>
<custom-input name='ID'></custom-input>