【问题标题】:change class css using jquery使用 jquery 更改类 css
【发布时间】:2015-06-26 21:34:43
【问题描述】:

我有一个使用媒体查询的类:

css:

@media only screen and (min-width: 58.75em)
.top-bar-section ul li {
  border-left: 1px solid #fff;
  transform: skewX(15deg);
}

现在,如果City() 的值是 London,我想将上面的 CSS 更改为:

 @media only screen and (min-width: 58.75em)
    .top-bar-section ul li {
      border-left: 1px solid #fff;
      transform: skewX(-15deg);
    }

我的 javascript 是:

if (City() == "London"){
//change the css class from the first example to the 2nd?
}

关于如何使用 jQuery 更改类 css 的任何帮助? 谢谢, 乔什

【问题讨论】:

  • 媒体查询条件相同!
  • 制作第二类并使用 removeClass 和 addClass
  • @legends 变换属性值不同
  • 欢迎来到 StackOverflow。您能否详细说明到目前为止您已经尝试过什么? jQuery 提供了一种addClass() 方法以及一种用于删除的方法——你试过了吗?
  • 如果对您有帮助,请参阅this

标签: javascript jquery css


【解决方案1】:

是的,您可以在 jQuery 的帮助下完成,如果指定条件为真,则添加 jQuery newClass,反之亦然使用 jQuery removeClass

【讨论】:

    【解决方案2】:

    您可以使用 addClass 和 removeClass 方法来制作它。

    【讨论】:

      【解决方案3】:

      指定css可以使用jQuery的.css()-function:

      $('.top-bar-section ul li').css({
        '-webkit-transform' : 'skewX(-15deg)',
        '-moz-transform'    : 'skewX(-15deg)',
        '-ms-transform'     : 'skewX(-15deg)',
        '-o-transform'      : 'skewX(-15deg)',
        'transform'         : 'skewX(-15deg)'
      });
      

      Demo

      或者您可以指定第二个类,例如:

      .top-bar-section ul li.london {
          transform: skewX(-15deg);
      }
      

      然后打电话

      $('.top-bar-section ul li').addClass('london');
      

      在你的 if 条件中。要删除课程,您可以使用.removeClass()

      参考

      .css()

      .addClass()

      .removeClass

      【讨论】:

        【解决方案4】:

        您可以使用带有类属性的 jQuery 更改类,这里是 documentation

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-08-07
          • 2017-11-12
          • 2012-07-13
          • 1970-01-01
          • 2015-02-21
          • 2010-10-11
          • 2011-09-01
          • 1970-01-01
          相关资源
          最近更新 更多