我怎样才能改变使用jQuery多个元素的风格?(How can I change the style

2019-10-18 12:45发布

我有一个CSS样式表,像这样的规则:

h1, h2, h3, h4, .contentheading, .title{

 font-size: 13px ;
 font-weight: normal;
 font-family: Arial, Geneva, Helvetica, sans-serif ;
}

这些标签,是由插件,所以我可以在一个类不能添加到它生成的类。

那么,有没有什么办法,我可以一次改变风格的所有元素,在运行时,并不需要通过他们去一个一个?

Answer 1:

你可以在jQuery的多个选择,就像在CSS。 也许不是最好的性能代价,但会奏效。

$('h1, h2, h3, h4, .contentheading, .title').css('color', 'red');
$('h1, h2, h3, h4, .contentheading, .title').addClass('someOtherClass');


Answer 2:

这是非常简单的使用jQuery。 只需使用有问题的选择为你的jQuery选择,那么更改CSS。 因此,使用Javascript / jQuery代码会是这样,假设你想改变font-sizefont-weight

$('h1, h2, h3, h4, .contentheading, .title').css({
  'font-size': '17px',
  'font-weight': 'bold'
});

这听起来像你是新来的jQuery,你可能想熟悉文档和API的网站。



Answer 3:

在CSS:

* {
    font-size: 13px;
    font-weight: normal;
    font-family: Arial, Geneva, Helvetica, sans-serif;
}


Answer 4:

$('h1,h2,h3,h4,.contentheading,.title').css({ 'color': '#fff' });


Answer 5:

由于这个问题是标签的jQuery,使用*选择应该帮助你获得的所有元素。 然后你可以使用css方法来改变风格。

$('*').css();

在CSS中你可以使用相同的*选择一些样式应用到所有元素,但要确保你把它放在最后一个CSS文件,以便覆盖所有其他规则。

* {
    font-size: 13px;
    font-weight: normal;
    font-family: Arial, Geneva, Helvetica, sans-serif;
}


文章来源: How can I change the style of multiple elements using jQuery?