Click the div to toggle the color code as follows:
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Document</title><style>. Test1{width:100px;Height:100px;Background-color:Green; }</style><Scripttype= "Text/javascript"> functionmyFunction () {vartest1=document.getElementById ('test1'); if(Test1.style.backgroundColor=='Yellow') {Test1.style.backgroundColor='Green'; }Else{Test1.style.backgroundColor='Yellow'; } }</Script></Head><Body> <Divclass= "Test1"ID= "Test1"alt=""onclick= "myFunction ()"> </Div></Body></HTML>
Here's a question: If the color code in the JS statement is hexadecimal, the effect is not displayed. There is no solution to this problem. Ask the great God to teach Qaq
Click the image to switch back and forth:
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title></title></Head><Body><Scripttype= "Text/javascript"> functionToggle () {vartest1=document.getElementById ('test1'); if(Test1.src.indexOf (' on') >= 0) {test1.src="./images/off.png"; }Else{test1.src="./images/on.png"; } }</Script> <imgID= "Test1"src= "./images/on.png"alt=""onclick= "Toggle ()" /></Body></HTML>
JavaScript Click div to switch back and forth two colors and two image switching methods