Jquery 包含各种方法,其中之一是 CSS()。CSS() 方法用于获取应用于特定 HTML 元素的特定 CSS 属性的值。此外,它还用于为特定 HTML 元素设置 CSS 属性及其值。开发人员还可以使用 CSS() 方法更新 CSS 属性值。
在本教程中,我们将学习使用 Jquery 的 css() 方法来访问和设置特定 HTML 元素的 CSS 属性。
语法
用户可以按照下面的语法来使用Jquery的css()方法。
Var value = $('element').css(property); $('element').css(property, value); $('element').css(property, function() { return value; }); $('element').css({property1: value1, property2: value2, ...});
css()方法接受一个或两个参数。在这里,'property'是要访问或设置其值的CSS属性名称。此外,它还接受包含多个CSS属性键值对的对象。
示例 1
在下面的示例中,我们为div元素设置了背景颜色。当用户点击按钮时,回调函数使用Jquery的CSS()方法来访问div元素的'background-color'属性值。
在输出中,用户可以在单击按钮后观察 RGB 值中 div 元素的背景颜色。
<html> <head> <style> .text { background-color: rgb(88, 236, 236); } </style> <script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.4/jquery.min.js"> </script> </head> <body> <h2 id="Using-the-i-CSS-method-i-of-JQuery-to-access-the-value-of-background-color">Using the <i> CSS() method </i> of JQuery to access the value of background-color</h2> <div class = "text"> This is a sample div element. </div> <h3 id="Click-the-below-button-to-get-the-background-color-of-the-above-div-element"> Click the below button to get the background color of the above div element. </h3> <button id = "btn"> Click Me </button> <div id = "output"> </div> <script> $('#btn').click(function () { var color = $('.text').css('background-color'); let output = document.getElementById('output'); output.innerHTML = "The background color of the div element is " + color; }); </script> </body> </html>
示例 2
在下面的示例中,我们使用css()方法为div元素设置背景颜色。在这里,当用户点击按钮时,回调函数使用其类名和css()方法访问div元素。我们将'background-color'作为第一个参数,属性名称,'red'作为第二个参数,属性值进行传递。
在输出中,用户可以观察到,当单击按钮时,div 元素的背景颜色变为红色。
<html> <head> <style> .text { background-color: rgb(31, 219, 163); } </style> <script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.4/jquery.min.js"> </script> </head> <body> <h2 id="Using-the-i-CSS-method-i-of-JQuery-to-set-the-value-of-background-color">Using the <i> CSS() method </i> of JQuery to set the value of background-color</h2> <div class = "text"> This is a sample div element. </div> <h3 id="Click-the-below-button-to-set-the-red-background-color-of-the-above-div-element"> Click the below button to set the red background color of the above div element. </h3> <button id = "btn"> Click Me </button> <script> $('#btn').click(function () { var color = $('.text').css('background-color', 'red'); }); </script> </body> </html>
示例 3
在下面的示例中,我们使用随机像素值更改div元素的填充。在这里,我们使用'padding'作为css()方法的第一个参数,并将函数作为css()方法的第二个参数。
在这个函数中,我们使用Math.random()方法来获取1到50之间的随机数,并将随机值返回以设置为HTML div元素的填充。在输出中,用户可以观察到随机的填充值。
<html> <head> <style> .text { background-color: rgb(31, 219, 163); } </style> <script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.4/jquery.min.js"> </script> </head> <body> <h2 id="Using-the-i-CSS-method-i-of-JQuery-to-get-css-property-value-from-the-callback-function-and-set-it">Using the <i> CSS() method </i> of JQuery to get css property value from the callback function and set it</h2> <div class = "text"> Welcome to the TutorialsPoint! </div> <h3 id="Click-the-below-button-to-set-the-custom-padding-for-the-above-div-element"> Click the below button to set the custom padding for the above div element. </h3> <button id = "btn"> Click Me </button> <div id = "output"> </div> <script> $('#btn').click(function () { var color = $('.text').css('padding', function () { // generate a random number between 0 to 50 var random = Math.floor(Math.random() * 50); let padding = random + 'px'; let output = 'The padding value is: ' + padding; $('#output').text(output); return padding; }); }); </script> </body> </html>
Example 4
的中文翻译为:示例4
在下面的示例中,我们使用CSS()方法将多个CSS属性设置给访问的HTML元素。在这里,我们将对象作为CSS()方法的参数传递。该对象包含多个CSS属性-值对。
当用户单击该按钮时,它会将所有 CSS 属性应用于 div 元素,用户可以在输出中看到该元素。
<html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.4/jquery.min.js"> </script> </head> <body> <h2 id="Using-the-i-CSS-method-i-of-JQuery-to-set-multiple-CSS-properties-to-the-element">Using the <i> CSS() method </i> of JQuery to set multiple CSS properties to the element</h2> <div class = "text"> Welcome to the TutorialsPoint! </div> <h3 id="Click-the-below-button-to-set-multiple-CSS-properties-to-the-above-div-element">Click the below button to set multiple CSS properties to the above div element.</h3> <button id = "btn"> Click Me </button> <div id = "output"> </div> <script> $('#btn').click(function () { var color = $('.text').css({ 'color': 'red', 'background-color': 'blue', 'font-size': '20px', 'border': '2px solid green', "width": "500px", "height": "50px", }); }); </script> </body> </html>
开发人员学习使用Jquery的css()方法。在第一个示例中,我们使用css()方法访问CSS属性值。在第二个示例中,我们将CSS属性设置为HTML元素。
在第三个示例中,我们将函数返回的值设置为CSS属性值。在最后一个示例中,我们使用CSS()方法将多个CSS属性值设置给HTML元素。
以上是jQuery 中 css() 方法有什么用?的详细内容。更多信息请关注PHP中文网其他相关文章!

在这篇文章中,布莱克·莫里(Blackle Mori)向您展示了一些骇客,同时试图推动同位HTML支持的极限。如果您敢于使用这些,以免您也被标记为CSS罪犯。

具有CSS的自定义光标很棒,但是我们可以将JavaScript提升到一个新的水平。使用JavaScript,我们可以在光标状态之间过渡,将动态文本放置在光标中,应用复杂的动画并应用过滤器。

互动CSS动画和元素相互启动的元素在2025年似乎更合理。虽然不需要在CSS中实施乒乓球,但CSS的灵活性和力量的增加,可以怀疑Lee&Aver Lee&Aver Lee有一天将是一场

有关利用CSS背景滤波器属性来样式用户界面的提示和技巧。您将学习如何在多个元素之间进行背景过滤器,并将它们与其他CSS图形效果集成在一起以创建精心设计的设计。

好吧,事实证明,SVG的内置动画功能从未按计划进行弃用。当然,CSS和JavaScript具有承载负载的能力,但是很高兴知道Smil并没有像以前那样死在水中

是的,让#039;跳上文字包装:Safari Technology Preview In Pretty Landing!但是请注意,它与在铬浏览器中的工作方式不同。

此CSS-tricks更新了,重点介绍了年鉴,最近的播客出现,新的CSS计数器指南以及增加了几位新作者,这些新作者贡献了有价值的内容。

在大多数情况下,人们展示了@Apply的@Apply功能,其中包括Tailwind的单个property实用程序之一(会改变单个CSS声明)。当以这种方式展示时,@Apply听起来似乎很有希望。如此明显


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Linux新版
SublimeText3 Linux最新版

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),