JavaScript에서 span 요소의 텍스트를 가져오려면 코드는 다음과 같습니다. -
예시
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { font-size: 18px; font-weight: 500; } </style> </head> <body> <h1>JavaScript Date Methods</h1> <span class="test">This is some sample text inside a span element</span> <div class="sample"></div> <button class="Btn">CLICK HERE</button> <h3> Click on the above button to get the span text </h3> <script> let sampleEle = document.querySelector(".sample"); let spanEle = document.querySelector(".test"); document.querySelector(".Btn").addEventListener("click", () => { sampleEle.innerHTML = "The span text is = " + spanEle.innerHTML; }); </script> </body> </html>
출력
"여기를 클릭" 버튼을 클릭하면 -