-
-
Save n00nietzsche/6195331c48dbdaccdc10e3563ee60b52 to your computer and use it in GitHub Desktop.
템플릿 태그 함수 예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const insertHTML = | |
(selector, { position = "beforeend", prefix = "", suffix = "" }) => | |
(strings, ...values) => { | |
const $selected = document.querySelector(selector); | |
if ($selected) { | |
const htmlString = strings.reduce((result, string, i) => { | |
result += string; | |
if (values[i]) { | |
result += prefix + values[i] + suffix; | |
} | |
return result; | |
}, ""); | |
$selected.insertAdjacentHTML(position, htmlString); | |
return; | |
} | |
console.warn( | |
`selector: ${selector} 에 해당하는 엘리먼트가 존재하지 않습니다.` | |
); | |
return; | |
}; | |
const content = "myButton"; | |
insertHTML("#elemId", { prefix: "(", suffix: ")" })` | |
<button>${content}</button> | |
`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
결과로
#elemId
아이디를 가진 DOM 요소에를 붙일 수 있다.