简体   繁体   English

如何使用jQuery使元素替换颜色?

[英]How do you make an element alternate colors with jQuery?

So I figured out how to make an element change once it is clicked--but then, I can't figure out how to make it change back to the original when it is clicked again. 因此,我想出了如何在单击元素后进行更改—但是,当再次单击元素时,我无法弄清如何将其更改回原始。

I am trying to get the gray pluses to change to green when they are clicked, and then back to gray if clicked again. 我试图使灰色加号在单击时变为绿色,然后再次单击则变回灰色。

我正在尝试制作其他颜色的优点

Make it change to one color: 将其更改为一种颜色:

$(document).ready(function(){
    $(document).on('click', '#add_image', function(){

        $(this).empty();
        $(this).append("<img src='images/green_plus.png' id='green'/>")
    });
})

My attempt to make it alternate: 我试图使其交替:

$(document).ready(function(){
    $(document).on('click', '#add_image', function(){

        $(this).empty();
        $(this).append("<img src='images/green_plus.png' id='green'/>")
        $(this).click(function(){
                $(this).empty();
                $(this).append("<img src='images/gray_plus.png' id='green'/>")
        });
    });
})

Also, here is the php which generated the list: 另外,这是生成列表的php:

foreach($schools as $school){
        echo "<tr>";
            echo "<td>";
                echo "<a href='#' id='add_image'><img src = 'images/gray_plus.png'/></a>";
            echo "</td>";       
            echo "<td>";
                echo ("<a href='school/?school_name=" . $school['school_name'] . "'>");
                echo $school['school_name'];
                echo "</a>";
            echo "</td>";
       echo "</tr>";
        }

toggleClass() is actaully what you need here. toggleClass()实际上是您在这里需要的。

create a class , one with an img green_plus and other with grey_plus.and on click change the class. 创建一个类,一个带有img green_plus,另一个带有grey_plus。然后单击更改该类。

CSS CSS

.greyplus{
  background-image: 'path/to/gryimg'
}
.greenplus{
  background-image: 'path/to/grnimg'
}

html HTML

...
echo "<a href='#' id='add_image' class='greyplus'></a>";
...

jquery jQuery的

$('#add_image').click(function(){
  $(this).toggleClass('greyplus').toggleClass('greenplus'); 
});

i don't think you need to use on unless you are adding it dynamically. 我认为除非动态添加on否则您不需要使用。

See you have to use classes instead of id because id should be unique for each element but we can use same class name to multiple instances of dom nodes. 看到您必须使用类而不是id,因为id对于每个元素应该是唯一的,但是我们可以对dom节点的多个实例使用相同的类名。

$(document).ready(function(){
  var i = 0;
  $(document).on('click', '.add_image', function (e) { // change id to class
    e.preventDefault();  //<----stop the default behavior
    var img = "";
    $(this).empty();
    if(i==0) { 
       img = "green_plus.png"
       i++; //<----------------update to put it here.
    }else{
       img = "gray_plus.png";
       i = 0;
    }
    $(this).append("<img src='images/"+img+"' class='green'/>"); // change id to class
  });
});

so you have to use class name instead of IDs, 因此您必须使用类名而不是ID,

I suggest using different classes for that. 我建议为此使用不同的类。 Make plus sign as background image. 使加号作为背景图像。 And then do jQuery logic: 然后执行jQuery逻辑:

$(document).on('click', '.myButton', function(){
    $(this)
        .toggleClass('redPlus') // class for clicked element
        .toggleClass('greenPlus'); // class for default element
});

add css for background image 为背景图像添加CSS

.original{
background-image: (image here)
}
.changed{
background-image: (another image)
}

on jquery just do as: 在jQuery上只是作为:

$(document).ready(function(){
    $(document).on('click', '#add_image', function(){
      if($(this).hasClass('original'){
         $(this).removeClass('original'); 
         $(this).addClass('changed');
      }
      else{
         $(this).addClass('original');
      } 
    });
})

I would set up appropriate CSS classes for each desired color images Ie .green with default selector being grey, then do this: 我将为每个所需的彩色图像(即.green)设置适当的CSS类,默认选择器为灰色,然后执行以下操作:

    $('body').on("click", "#add_image", function() {
        $(this).toggleClass("green");
    });

Then use CSS before pseudo selectors to put the image beside the clickable element in the CSS classes themselves Ie .green. 然后在伪选择器之前使用CSS将图像放在CSS类本身即Green中的clickable元素旁边。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM