简体   繁体   English

如何使用.css()向元素添加多个CSS样式

[英]How to add multiple CSS styles to an element with .css()

I'm aware on how to add styles to nested elements using jQuery and JavaScript but for some reason I can't figure out how to add a style to a deeply nested element. 我知道如何使用jQuery和JavaScript为嵌套元素添加样式但由于某种原因我无法弄清楚如何将样式添加到深层嵌套元素。

I'm using WordPress for my site and it has added a bunch of divs around the element that I am trying to reach. 我正在为我的网站使用WordPress,它在我试图达到的元素周围添加了一堆div。

I am trying to style the element with the class name .bx-wrapper . 我正在尝试使用类名.bx-wrapper设置元素的样式。 However, I only want to style the .bx-wrapper that is nested inside of the class .associate-wrapper . 不过,我只想样式的.bx-wrapper一个嵌套类的内部.associate-wrapper

Here is my current HTML: 这是我目前的HTML:

<div class="row associate-wrapper">
  <li id="black-studio-tinymce-16" class="widget">
    <div class="textwidget">
      <div class="row"></div>
      <div class="col-md-12 text-center"></div>
      <div class="col-md-12 text-center">
        <div class="bx-wrapper">
          <p>content....</p>
        </div>
      </div>
    </div>
  </li>
</div>

And here is my current non-working jQuery: 这是我目前的非工作 jQuery:

$('.associate-wrapper').find('.bx-wrapper').css('position', 'absolute', 'margin-top', '30px');

Since you are adding more than one property to your jQuery .css() tag, you will need to make slight adjustments to your .css() syntax like this: 由于您要在jQuery .css()标记中添加多个属性,因此需要对.css()语法稍作调整,如下所示:

$('.associate-wrapper').find('.bx-wrapper').css({'position': 'absolute', 'margin-top': '30px'});

JSFiddle with above script JSFiddle上面的脚本


Or you can just change the selectors the same way CSS does and as mentioned above, since you are adding more than one property to your jQuery .css() tag, you will have to write your script like this: 或者,你可以改变以同样的方式做的CSS 上面提到的选择,因为你要添加多个属性到您的jQuery .css()标签,你将不得不写这样的脚本:

$('.associate-wrapper .bx-wrapper').css({"position": "absolute", "margin-top": "30px"});

JSFiddle with above script JSFiddle上面的脚本

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

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