How can I click on the background to change blue to red and red to blue? What's wrong with my code? Help, thank you
思如磐石
思如磐石 2017-12-13 17:20:50
0
6
1639


How do I click on the background to change blue to red and red to blue? What's wrong with my code? Help, thank you

QQ图片20171213171840.jpg


##<!DOCTYPE html>

<html>

<head>

</head>

<style>

#test1 {

width:300px;

height:300px;

background:blue;

}

#test2 {

width:300px;

height: 300px;

background:red;

}

</style>

<body>

<div id ="test1" onclick="te()"></div>


##</body>

<script>

function te(){

var a = document.getElementById('test1');

if(a.id == 'test1'){

a.id = 'test2';

}else

a.id = 'test1';

}

</script>

</html>


思如磐石
思如磐石

reply all(5)
ganhuihui
<!DOCTYPE html>

<html>

<head>

</head>

<style>

#test1 {

width:300px;

height:300px;

background-color:blue;

}

</style>

<body>

<div id="test1"></div>



</body>

<script>

    function test1() {
        var a = document.getElementById('test1');
var finalStyle = a.currentStyle ? a.currentStyle : document.defaultView.getComputedStyle(a, null);/*利用判断是否支持currentStyle(是否为ie)
13     来通过不同方法获取style*/
if(finalStyle.backgroundColor=="rgb(0, 0, 255)"){

a.style.backgroundColor="red"; 
}
else if(finalStyle.backgroundColor=="rgb(255, 0, 0)"){

a.style.backgroundColor="blue"; 

}

      }
  
function addEvent(obj,type,handle){
    try{  // Chrome、FireFox、Opera、Safari、IE9.0及其以上版本
        obj.addEventListener(type,handle,false);
    }catch(e){
        try{  // IE8.0及其以下版本
            obj.attachEvent('on' + type,handle);
        }catch(e){  // 早期浏览器
            obj['on' + type] = handle;
        }
    }
}

 window.onload = function(){
        var element = document.getElementById("test1");
        addEvent(element,"click",test1);
      }
</script>

</html>


ganhuihui
#test1 {

width:300px;

height:300px;


  • reply &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;style&gt; #test1 { width:300px; height:300px; background-color:blue; } &lt;/style&gt; &lt;body&gt; &lt;div id="test1"&gt;&lt;/div&gt; &lt;/body&gt; &lt;script&gt; function test1() { var a = document.getElementById('test1'); var finalStyle = a.currentStyle ? a.currentStyle : document.defaultView.getComputedStyle(a, null);/*Use to determine whether currentStyle is supported (whether it is ie) 13. Get style through different methods*/ if(finalStyle.backgroundColor=="rgb(0, 0, 255)"){ a.style.backgroundColor="red"; } else if(finalStyle.backgroundColor=="rgb(255, 0, 0)"){ a.style.backgroundColor="blue"; } } function addEvent(obj,type,handle){ try{ // Chrome, FireFox, Opera, Safari, IE9.0 and above obj.addEventListener(type,handle,false); }catch(e){ try{ // IE8.0 and below obj.attachEvent('on' + type,handle); }catch(e){ // Early browsers obj['on' + type] = handle; } } } window.onload = function(){ var element = document.getElementById("test1"); addEvent(element,"click",test1); } &lt;/script&gt; &lt;/html&gt;
    ***huihui author 2018-01-18 14:31:01
有人@我
  1. You can use addinventlisner() to listen for click events, initialize it to red, change it to blue when you click it for the first time, and then turn it to red when you click it again

  2. ##Preparation A class{background-color:#f00}, when you click it, add a class name to it, and when you click it again, let it remove the class name

  3. You can use move in Remove event


p~ang~胖

A is undefined when executed for the second time. Just put var a = document.getElementById('test1'); outside the function

dabour

Shouldn’t just change one ID, if the match is successful, another one will be displayed

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!