id 예제
<style> .code { display: block; padding: 5px; background-color: gray; color: white; } #hz { background-color: tomato; } </style> <a class="code" href="https://homzzang.com/b/html/" id="hz">HTML</a> <a class="code" href="https://homzzang.com/b/css/">CSS</a> <a class="code" href="https://homzzang.com/b/js/">JS</a> <button onclick="homzzang()">클릭</button> <p id="demo"></p> <script> function homzzang() { var x = document.getElementsByClassName("code")[0].id; document.getElementById("demo").innerHTML = x; } </script> 결과보기
<style>
.code {
display: block;
padding: 5px;
background-color: gray;
color: white;
}
#hz {
background-color: tomato;
</style>
<a class="code" href="https://homzzang.com/b/html/" id="hz">HTML</a>
<a class="code" href="https://homzzang.com/b/css/">CSS</a>
<a class="code" href="https://homzzang.com/b/js/">JS</a>
<button onclick="homzzang()">클릭</button>
<p id="demo"></p>
<script>
function homzzang() {
var x = document.getElementsByClassName("code")[0].id;
document.getElementById("demo").innerHTML = x;
</script>
결과보기