【问题标题】:How style two class css with jquery如何用 jquery 样式化两个类 css
【发布时间】:2020-11-25 18:34:50
【问题描述】:

我正在尝试不使用 file.css 而是使用 file.js 然后通过 jquery 插入 css 样式,您能帮帮我吗?

  $( document ).ready(function(){
      $( ".ytp-gradient-top", ".ytp-gradient-bottom" ).css({  
          "border": "3px solid red" 
      )};
  )};

【问题讨论】:

  • 你将不得不分别设置border-widthborder-styleborder-color
  • 您的意思是$( ".ytp-gradient-top,ytp-gradient-bottom" ).css 吗?
  • jQuery 只会触及第一个选择器(并默默地忽略第二个),因此您应该将您的选择器组合成一个像 freen-m 建议的那样(不过,在 ytp-gradient-bottom 之前添加点)。
  • 你也应该只传递 一个 字符串,两个类选择器用逗号分隔。

标签: jquery css wordpress


【解决方案1】:
$( ".ytp-gradient-top, .ytp-gradient-bottom" ).css( 
      "border" , "3px solid red" 
);

【讨论】:

  • 这与OP所拥有的基本相同。 jQuery 允许传递一个对象来设置多个属性,但只设置一个就可以了。
  • 除了删除document.ready,你能解释一下你做了什么吗?如果只是删除 document.ready,你能解释一下为什么你认为这就是答案吗?
  • @freedomn-m op 将属性添加为对象,而不是逗号分隔的参数。他的括号看起来也不匹配。我删除 'document.ready' 没有意义——问题不是document.ready,那部分代码不相关
  • @freedomn-m 我也将 css 选择器更改为 1 个字符串
  • 刚刚测试过,可以确认这个答案可以正常工作。
【解决方案2】:
  $( document ).ready(function(){

      $( ".ytp-gradient-top, .ytp-gradient-bottom" ).css({

          "border" : "3px solid red" 

    )};

)};

您需要在选择器和逗号周围添加引号。请参阅 jQuery API:https://api.jquery.com/multiple-selector/

【讨论】:

    猜你喜欢
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 2021-09-19
    相关资源
    最近更新 更多