Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用更改DIV背景色。单击(函数(){_Jquery_Colors_Background_Click - Fatal编程技术网

Jquery 使用更改DIV背景色。单击(函数(){

Jquery 使用更改DIV背景色。单击(函数(){,jquery,colors,background,click,Jquery,Colors,Background,Click,如何使用jQuery更改背景色。单击(函数(){ div normal with Green color,单击时更改为红色背景色 <div class="stileone"> Div Content </div> Div内容 您需要在div中找到onclick事件,然后使用 .css("background-color:#00000;); 使用.on()。当事件发生时,在上绑定一个事件,然后使用$(this))引用函数中的对象,并在每次触发事件时切换类添加/

如何使用jQuery更改背景色。单击(函数(){

div normal with Green color,单击时更改为红色背景色

<div class="stileone">
   Div Content
</div>

Div内容

您需要在div中找到onclick事件,然后使用

.css("background-color:#00000;);
使用
.on()
。当事件发生时,在
上绑定一个事件,然后使用
$(this)
)引用函数中的
对象,并在每次触发事件时切换类添加/删除该类

jQuery:

$('.stileone').on('click',function(){
  $(this).css('backgroundColor','red');
  // OR: if you want to work with a class
  $(this).toggleClass('redClass');
});
css:


我知道这里有很多答案,但我想我会指出,有很多方法可以实现这一点,而且很多方法的目的稍有不同

基本CSS Psuedo类旨在处理类似的内容,但它们并不总是跨浏览器兼容。例如,我认为以下内容在IE>=8中不起作用,但在Chrome和IE9中起作用。

然后当然单击“基本”以更改css。当然,这将使更改成为最基本的更改,而无需任何其他命令。

如前面的回答所示,存在单击切换类。在这种情况下,您可以使用所需的背景创建一个类,然后根据单击操作打开和关闭该类。

如果您需要jQuery方式来导入
:active
,那么您可以使用鼠标向下/向上而不是简单的单击。这将允许您有效地“跨浏览器”导入CSS的:active`功能。

最后,我现在能想到的最后一个特性是使用最新的jQuery(1.8+)中目前已被破坏的方法,然而,在所有其他jQuery(1.72-)中,它似乎工作正常。它被称为“切换”方法,通常用于定义aa元素的显示和隐藏。但是在旧版本中,它有一个替代功能,您可以在其中定义回调,从而创建自己的动画。


您是想让它在单击时只闪烁红色吗?如“活动”?好的,使用“切换”怎么办?如果默认颜色为绿色,则单击时为红色,但当单击任何其他div或链接时,绿色将返回感谢您的支持,还有一个问题,请单击“div Content 2”将“div Content 1”更改为绿色谢谢1,解释得很好!谢谢你花时间帮助社区
$('.stileone').on('click',function(){
  $(this).css('backgroundColor','red');
  // OR: if you want to work with a class
  $(this).toggleClass('redClass');
});
.redClass {
background-color: red;
}
div:active { background-color: red; }
$("div").on("click", function() {
    $(this).css("background-color", "red");
});
//  OR
$("div").on("click", function() {
    $(this).css({ backgroundColor: "red" });
});
$("div").on("click", function() {
    $(this).toggleClass('back-red');
});
$("#div5").on("mousedown", function() {
    $(this).toggleClass('back-red');
})
.on("mouseup", function(e) {
    $(this).toggleClass('back-red');
});
$("div").toggle(function() {
    $(this).css("background-color", "red");
}, 
function() {
    $(this).css("background-color", "");
});