javascript에 비해 html 객체 하나하나 다루기가 간편하고 쉽다.
html 메소드는 html 태그 형태로 가져온다.
안에 값을 넣을 경우 html로 쓴다
text 메소드는 html 태그를 다 벗긴 채 text만 가져온다.
input 안에 있는 value값 등을 가져오거나 조작할 때 쓴다.
$("<p>안녕하세요</p>");
이런식으로 작성한다.
remove()는 해당 노드(태그)를 지워버리는 것이고, empty()는 해당 노드(태그)의 내부를 지운다.
코드
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script> <script> $(document).ready(()=>{ setTimeout(function timer(){ const date = new Date(); const str = `${("00"+date.getHours()).slice(-2)}:${("00"+date.getMinutes()).slice(-2)}:${("00"+ date.getSeconds()).slice(-2)}`; $("body > div").html(str); setTimeout(timer, 1000); },1000 ); }); </script> </head> <body> <div></div> </body> </html>
실행 결과
좋은 글 감사합니다!