Is it possible to change the background color and back to the original in plain javascript every time i click a button?

前端 未结 2 385
清酒与你
清酒与你 2021-01-29 01:49

I can\'t seem to see any results on google unless I use jQuery and I\'m not so sure if jQuery and javascript plain work together.

2条回答
  •  孤城傲影
    2021-01-29 02:00

    jQuery will not conflict with javascript, it is just a js library :) So you can use the selector.css() of jquery or, in standard js i think it is (element).style.background = color.

    I recommend using jQuery. Here is a SIMPLE jQuery way to do it:

      $("#button").click(function(){ 
       $("#tochange").css("background-color", "colorhex");
      });
    

    If you want to swap colors you could, for example, add a variable.

    var x;
    $("#button").click(function(){
    if(x==0){
        $("#tochange").css("background-color", "colorhex"); 
        x= 1;
     } 
     else{other way round} 
    });
    

提交回复
热议问题