这个 JQuery 库几乎支持级联样式表(CSS)中包含的所有选择器。这个 css() 方法 JQuery 用于更改选定图元的样式属性。这个 css() 在JQuery中,可以以不同的方式使用。 css()方法可用于检查所选元素属性的当前值:
null
语法:
$(selector).css(property)
返回值: 它将返回所选元素的属性值。
例1:
Input: $("p").css("color");Output: Output of the above input will return the rgb() value of the element.
代码#1:
HTML
<!DOCTYPE html> < head > /jquery/3.3.1/jquery.min.js"> // this is the link of JQuery CDN direct from the // jquery website so that we can use all the //function of JQuery css() </ script > </ head > < body > < button >Click here and it will return the color value of p element</ button > < p style = "color:green" >Wecome to gfg!</ p > </ body > < script > $(document).ready(function() { //here selecting button element $("button").click(function() { // here when the button is clicked css() method // will return the value using alert method alert($("p").css("color")); }); }); </ script > </ html > |
输出: 在点击按钮之前-
点击按钮后-
css()方法还用于添加或更改选定元素的属性。
语法:
$(selector).css(property, value)
返回值: 这将更改选定图元的属性值。
例2:
Input: $("p").css("color", "red");Output: Output of the "p" element becomes red whatever may be the color previously.
代码#2:
HTML
<!DOCTYPE html> < head > /jquery/3.3.1/jquery.min.js"> // this is the link of JQuery CDN direct from // the jquery website so that we can use all // the function of JQuery css() </ script > </ head > < body > < button >Click here and it will return the color value of p element</ button > < p style = "border: 2px solid green;color:red;padding:5px" > Wecome to gfg!.</ p > </ body > < script > $(document).ready(function() { // here selecting button element $("button").click(function() { // here when the button is clicked css() // method will change the color of paragraph $("p").css("color", "green"); }); }); </ script > </ html > |
输出: 在点击按钮之前-
点击按钮后-
css()方法可以使用函数更改选定元素的css属性:
语法:
$(selector).css(property, function(index, currentvalue))
返回值: 它将返回所选属性的更改值。
例3:
Input: $("p").css("padding", function(i){ return i+20;});Output: Output will get is the paragraph with padding value increases to "25px" whatever be the initial value.
代码#3:
HTML
<!DOCTYPE html> < head > /jquery/3.3.1/jquery.min.js"> //this is the link of JQuery CDN direct from //the jquery website so that we can use all //the function of JQuery css() </ script > </ head > < body > < button >Click here and the padding will change.</ button > < p style = "border: 2px solid green;color:green;padding=5px;" > Welcome to gfg!.</ p > </ body > < script > $(document).ready(function() { $("button").click(function() { $("p").css("padding", function(h) { return h + 30; }); }); }); </ script > </ html > |
输出: 在点击按钮之前-
点击按钮后-
在css方法的帮助下,我们还可以在JQuery中一次应用多个属性。
注意:在这个方法中,我们用camelCase写属性名。
语法:
$(selector).css({property:value, property:value, ...})
代码#4:
HTML
<!DOCTYPE html> < html > < head > < title >Document</ title > </ head > < body > < p style = "border: 2px solid green;color:green;padding=5px;" > Welcome to gfg!.</ p > < button >Apply css</ button > < script src = </ script > < script > $("button").click(function(){ //applying more than one property at a time //Note : property name written in camelCase $("p").css({"backgroundColor":"green", "color":"white","fontSize":"20px"}); }); </ script > </ body > </ html > |
输出:
在点击按钮之前-
点击按钮后-
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END