简体   繁体   English

在jQuery中切换内联CSS

[英]Toggle inline CSS in jQuery

I want to toggle some inline CSS with a jQuery script, but I can't do it with a class, because I get the value of the padding-top dynamically, here is the function : 我想用jQuery脚本切换一些内联CSS,但是我不能用类来做,因为我动态得到了padding-top的值,这里是函数:

$('.button').click(function(){
  $(this).toggleClass('active');
});

var tagsHeight = $('span').outerHeight();

$(".button").click(function (){
  if ($(this).is('active')) {
    $(".change").css('padding-top', '0');
  }
  else if ($(this).not('active')) {
    $(".change").css('padding-top', tagsHeight);
  }
});

An a example here : https://jsfiddle.net/o1pbwfuo/ 这里有一个例子: https//jsfiddle.net/o1pbwfuo/

I really don't get why this is not working correctly ... 我真的不明白为什么这不能正常工作......

Thanks ! 谢谢 !

The issue with your code is due to the incorrect selector in not() . 您的代码的问题是由于not()选择器not()正确。 That being said, you can improve your logic by combining the click() event handlers, then using a single ternary expression to set the padding-top on the required element based on the related class. 话虽这么说,您可以通过组合click()事件处理程序来改进逻辑,然后使用单个三元表达式根据相关类在所需元素上设置padding-top Try this: 尝试这个:

var tagsHeight = $('span').outerHeight();

$('.button').click(function() {
  var active = $(this).toggleClass('active').hasClass('active');
  $(".change").css('padding-top', !active ? '0' : tagsHeight);
});

Working example 工作实例

You made a mistake while using .is and .not. 你在使用.is和.not时犯了一个错误。 You need to address the class itself inclusive the dot at beginning. 您需要在开头处理包含点的类本身。

$('.button').click(function(){
  $(this).toggleClass('active');
});

var tagsHeight = $('span').outerHeight();

$(".button").click(function (){
  if ($(this).is('.active')) {
    $(".change").css('padding-top', '0');
  }
  else if ($(this).not('.active')) {
    $(".change").css('padding-top', tagsHeight);
  }
});

https://jsfiddle.net/06ek4fej/ https://jsfiddle.net/06ek4fej/


By the way, the else-if request is nonsense. 顺便说一句,else-if请求是无意义的。 If = true or if = false. If = true或if = false。 Else If results the same as else. 否则如果结果与其他相同。

$(".button").click(function (){
  if ($(this).is('.active')) {
    $(".change").css('padding-top', '0');
  }
  else {
    $(".change").css('padding-top', tagsHeight);
  }
});

On click you can check whether element has active class or not and there is no need to add two click methods on '.button'. 单击时,您可以检查元素是否具有活动类,并且无需在“.button”上添加两个单击方法。

var tagsHeight = $('span').outerHeight();

$(".button").click(function (){
  $(this).toggleClass('active');
  if ($(this).hasClass('active')) {
    $(".change").css('padding-top', '0');
  }
  else{
    $(".change").css('padding-top', tagsHeight);
  }
});

You can use Has class method for the check is active class exist or not. 您可以使用Has类方法检查是否存在活动类。

$(".button").click(function (){
  if ($(this).hasClass('active')) {
    $(".change").css('padding-top', '0');
  }
  else{
    $(".change").css('padding-top', tagsHeight);
  }
});

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

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