Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.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
Javascript 第二次点击;jQuery 试验 $('.example')。单击(函数(){ $(this.css('color','red'); });_Javascript_Jquery - Fatal编程技术网

Javascript 第二次点击;jQuery 试验 $('.example')。单击(函数(){ $(this.css('color','red'); });

Javascript 第二次点击;jQuery 试验 $('.example')。单击(函数(){ $(this.css('color','red'); });,javascript,jquery,Javascript,Jquery,单击上面的代码时,它将应用.css。现在我需要的是应用另一位代码(比如$(this).css('color','blue');),在第二次单击时替换先前的代码 我已经搜索过了,询问者似乎只需要。show/。hide可以用替换的事件。toggle,这显然不是这里的情况。由于类示例的实例可能很多,因此简单地使用单个变量维护状态是不可行的,您可以做的是在其自身内维护example的每个实例的状态: <div class="example"> Test </div> $('.

单击上面的代码时,它将应用
.css
。现在我需要的是应用另一位代码(比如
$(this).css('color','blue');
),在第二次单击
时替换先前的代码


我已经搜索过了,询问者似乎只需要
。show
/
。hide
可以用
替换的事件。toggle
,这显然不是这里的情况。

由于类
示例
的实例可能很多,因此简单地使用单个变量维护状态是不可行的,您可以做的是在其自身内维护
example
的每个实例的状态:

<div class="example">
 Test
</div>

$('.example').click(function(){
 $(this).css('color','red');
});
定义两个css类

var isRed=false;
$('.example').click(function(){
 if(isRed)
 {
 $(this).css('color','blue');
 isRed=false;
 }
 else
 {
 $(this).css('color','red');
 isRed=true;
 }
});
然后单击您的方法:

.example { background:blue }
.example.red { background:red }
如果您不想定义新的css类,可以使用
data()
,以确保每个
示例中的状态是独占的。如果您有许多
实例,这将非常有用。示例

$('.example').click(function(){
 $(this).toggleClass('red');
});

使用addClass和removeClass

$('.example').click(function() {
    var color = $(this).data('color');
    if(color != 'blue') {
       $(this).css('color', 'blue');
       $(this).data('color', 'blue');
    } else {
       $(this).css('color', 'red');
       $(this).data('color', 'red');
    }
});
并在javascript中使用addClass和removeClass函数:

.blueColor
{
background-color: blue;
}

.redColor
{
background-color: red;
}

$(文档).ready(函数(){
$(“.example”).keypress(函数(){
如果($(“.example”).val().length>0)
{
$(“.example”).addClass(“红色”);
}
否则{
if($(“.example”).val().length==0)
{
$(“.example”).addClass(“蓝色”);
$(“.example”).removeClass(“红色”);
}
}
});
});

我想您需要更一般的关于单击事件的信息,所以我建议您使用
数据
方法来保留标记

<script>

    $(document).ready(function(){
    $(".example").keypress(function() {
    if($(".example").val().length > 0)
    {
        $(".example").addClass("redColor");
    }
    else {
       if($(".example").val().length == 0) 
    {
    $(".example").addClass("blueColor");
    $(".example").removeClass("redColor");
    }
    }

    });
    });
    </script>

类似的功能可以在两种颜色(或样式)之间切换


您好,请尝试使用
切换按钮(
:))

因此,您可以在css中使用“切换”,每单击一秒就会产生相反的效果。:)

代码

$('.example').click(function(){  

     if($(this).css('color') == "red")
     {
        $(this).css('color','blue');
     }
     else
     {
        $(this).css('color','red');
     }
}); 
祝你有个好男人,干杯

试试这个:

  $(function() {
   $('.example').toggle(function() {
    $(this).css('color','red');
   }, function() {
    $(this).css('color','blue');
   });
  });

使用one方法处理一次性事件绑定是一个不错的选择,但是此解决方案将在此代码之后停止所有绑定的事件,这可能会导致不一致

$('.example').click(function(){
      if($('.example').data('isAlreadyClicked')=='true')
      {
             $(this).css('color','blue');
             $('.example').data('isAlreadyClicked','false')
      } 
      else
      {
            $(this).css('color','red');
            $('.example').data('isAlreadyClicked','true') 
      }

});

很多答案都定义了一个单一的解决方案

基本上,您应该使用两种方法。提到的其他方法要么性能不佳,要么不可靠(对于这种解决方案,使用
data
是过分的)。以下是您可以使用的两种方法:

$('.example')
  .one('click', function(e) {
    e.stopImmediagePropagation();
  })
  .on('click', function() {
    $(this).css('color', blue');
  });
你可以写:

// Toggle a class 'red' defined in your stylesheet
$('.example').on('click', function() {
    $(this).toggleClass('red')
})

// Toggle the color with an "if" check
$('.example').on('click', function() {
    if (this.style.color === 'red') { // using jQuery is not required
        this.style.color === 'blue'
    }
    else {
        this.style.color === 'red'
    }
})

你想(例如)在蓝色和红色之间切换吗?因为你可以做一个
if
语句来测试颜色是什么,然后从中应用更改。谢谢,如果我只是在两个选项之间切换,我发现这是一个简单的解决方案。
$('.example').click(function(){
      if($('.example').data('isAlreadyClicked')=='true')
      {
             $(this).css('color','blue');
             $('.example').data('isAlreadyClicked','false')
      } 
      else
      {
            $(this).css('color','red');
            $('.example').data('isAlreadyClicked','true') 
      }

});
$('.example')
  .one('click', function(e) {
    e.stopImmediagePropagation();
  })
  .on('click', function() {
    $(this).css('color', blue');
  });
// Toggle a class 'red' defined in your stylesheet
$('.example').on('click', function() {
    $(this).toggleClass('red')
})

// Toggle the color with an "if" check
$('.example').on('click', function() {
    if (this.style.color === 'red') { // using jQuery is not required
        this.style.color === 'blue'
    }
    else {
        this.style.color === 'red'
    }
})
$('#id').toggle(function() {

  $('.css').css('color','red');}

,function() {           /////////the second click

   $('.css').css('color','blue');}
);