연습을하고있는 동안 W3School에서 JS 연습 중입니다. 두 버튼의 기능을 변경하고 싶었습니다. 내가하고 싶었던 무엇
JavaScript에서 버튼의 기능 변경
는 클릭에 라벨을 교환 할 다른 버튼 "라이트 켜기"를 추가했다 및 온 클릭 행동과 함께 "빛을 끄십시오".
<!DOCTYPE html>
<html>
<body>
<h1>What Can JavaScript Do?</h1>
<p><b><i>JavaScript can change HTML attributes.</i></b></p>
<p>In this case JavaScript changes the src (source) attribute of an image.</p>
<!-- Button1 (LHS) -->
<button id="buttonId1" onclick="document.getElementById('myImage').src='pic_bulboff.gif'">Turn off the light</button>
<!-- Image of the Bulb -->
<img id="myImage" src="pic_bulbon.gif" style="width:100px">
<!-- Button2 (RHS) -->
<button id="buttonId2" onclick="document.getElementById('myImage').src='pic_bulbon.gif'">Turn on the light</button>
<!-- Function to interchange label of both buttons along with the onclick actions -->
<script>
function clickButton() {
<!-- initially button1 is for switching off the button, changing it to turn-on state -->
document.getElementById('buttonId1').innerHTML = 'Turn on the light';
document.getElementById('buttonId1').onclick="document.getElementById('myImage').src='pic_bulbon.gif' " ;
<!-- initially button2 is for switching on the button, changing it to turn-off state -->
document.getElementById('buttonId2').innerHTML = 'Turn off the Light';
document.getElementById('buttonId2').onclick="document.getElementById('myImage').src='pic_bulboff.gif' " ;
}
</script>
<br>
<!-- Adding new button to manipulate button functionality -->
<button onclick="clickButton()" >Do Not Click Me</button>
</body>
</html>
, 그것은 을 클릭에 라벨을 대체 버튼 "나를 클릭하지 마세요"가 아니라 온 클릭 동작을 변경 않습니다!
해당 기능을 얻으려면 도움이 필요합니다.
하지만 Button 액션을 사용하면 onclick이 문자열로 매핑되고 그 시간에 작동하므로 왜 document.getElementById ('buttonId2')에서 작동하지 않습니다? onclick? – user308967