돌아가기
이 글은 다음 언어로만 작성되어 있습니다. English, Español, فارسی, Indonesia, Italiano, 日本語, Русский, Українська, 简体中文. 한국어 번역에 참여해주세요.

Add a closing button

중요도: 5

There’s a list of messages.

Use JavaScript to add a closing button to the right-upper corner of each message.

The result should look like this:

샌드박스를 열어 정답을 작성해보세요.

To add the button we can use either position:absolute (and make the pane position:relative) or float:right. The float:right has the benefit that the button never overlaps the text, but position:absolute gives more freedom. So the choice is yours.

Then for each pane the code can be like:

pane.insertAdjacentHTML("afterbegin", '<button class="remove-button">[x]</button>');

Then the <button> becomes pane.firstChild, so we can add a handler to it like this:

pane.firstChild.onclick = () => pane.remove();

샌드박스를 열어 정답을 확인해보세요.