可以使用 jQuery css()方法将一种或多种样式添加到 HTML 元素。甚至我们可以使用我们想要添加的所需样式创建一些自定义 CSS 类,并使用addClass()方法添加该类。在本文中,我们将看到添加一个或多个样式属性的两种方法。
css() 方法: css()方法用于更改所选元素的样式属性。 JQuery 中的css()可以以不同的方式使用。
句法:
-
对于单一样式:
$('selector').css('property':'value');
-
对于多种样式:
$('selector').css({ 'property1': 'value1', 'property2': 'value2' });
示例 1:
HTML
GeeksforGeeks
jQuery is one of the powerful libraries
of javascript which has many
powerful
methods that make the
manipulation of
DOM much simpler
using the selectors and
makes the
interaction with DOM much easier.
HTML
GeeksforGeeks
jQuery addClass() method
jQuery is one of the powerful libraries
of javascript which has many
powerful
methods that make the
manipulation of
DOM much simpler
using the selectors and
makes the
interaction with DOM much easier.
输出:
使用 addClass() 方法: addClass()方法用于将类添加到特定元素。我们可以为添加的类添加一些 CSS 属性。
句法:
-
对于单班:
$('selector').addClass('class_name');
-
对于多个类:
$('selector').addClass('class_name1 class_name2');
示例 2:
HTML
GeeksforGeeks
jQuery addClass() method
jQuery is one of the powerful libraries
of javascript which has many
powerful
methods that make the
manipulation of
DOM much simpler
using the selectors and
makes the
interaction with DOM much easier.
输出: