欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery实现获取及设置CSS样式操作详解

程序员文章站 2023-08-20 23:12:14
本文实例讲述了jquery实现获取及设置css样式操作。分享给大家供大家参考,具体如下: addclass():向被选元素添加一个或多个类 removecl...

本文实例讲述了jquery实现获取及设置css样式操作。分享给大家供大家参考,具体如下:

  • addclass():向被选元素添加一个或多个类
  • removeclass():从被选元素删除一个或多个类
  • toggleclass():对被选元素进行添加/删除类的切换操作
  • css():设置或返回被选元素的一个或多个样式属性

样式表实例:

.important{
font-weight:bold;
font-size:xx-large;
}
.blue{
color:blue;
}

addclass()

<!doctype html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").addclass("blue");
$("div").addclass("important");
});
});
</script>
<style type="text/css">
.important{
font-weight:bold;
font-size:xx-large;
}
.blue{
color:blue;
}
</style>
</head>
<body>
<h1>she is gone</h1>
<h2>no say anymore</h2>
<p>but i miss you</p>
<div>can you come back</div>
</br>
<button>addclass</button>
</body>
</html>

运行效果:

jQuery实现获取及设置CSS样式操作详解

可以在addclass()方法中规定多个类:

$("button").click(function(){
$("#div1").addclass("important blue");
});

removeclass()

<!doctype html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").removeclass("blue");
});
});
</script>
<style type="text/css">
.important{
font-weight:bold;
font-size:xx-large;
}
.blue{
color:blue;
}
</style>
</head>
<body>
<h1 class="blue">is she ?</h1>
<h2 class="blue">i do not know</h2>
<p class="blue">queit</p>
<br>
<button>removeclass</button>
</body>
</html>

运行结果:

jQuery实现获取及设置CSS样式操作详解

toggleclass()

<!doctype html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").toggleclass("blue");
});
});
</script>
<style type="text/css">
.blue{
color:blue;
}
</style>
</head>
<body>
<h1>who are you</h1>
<h2> i five thank you</h2>
<p>goodbye</p>
<button>toggleclass</button>
</body>
</html>

运行结果:

jQuery实现获取及设置CSS样式操作详解

css()

返回css属性

css("propertyname")

<!doctype html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("background color = " + $("#p2").css("background-color"));
});
});
</script>
</head>
<body>
<h2>我心在南朝</h2>
<p id=p1 style="background-color:#ff0000">田野上</p>
<p id=p2 style="background-color:#00ff00">红花盛开</p>
<p id=p3 style="background-color:#0000ff">玲珑剔透</p>
<button>css()</button>
</body>
</html>

运行结果:

jQuery实现获取及设置CSS样式操作详解

设置css属性

css("propertyname","value");

<!doctype html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").css("background-color","yellow");
});
});
</script>
</head>
<body>
<h2>我心在南朝</h2>
<p style="background-color:#ff0000">田野上</p>
<p style="background-color:#00ff00">红彤彤的花儿</p>
<p style="background-color:#0000ff">玲珑剔透</p>
<p>我心在南朝</p>
<button>设置css属性</button>
</body>
</html>

运行结果:

jQuery实现获取及设置CSS样式操作详解

设置多个css属性

$("p").css({"background-color":"yellow","font-size":"200%"});

jquery——尺寸

  • width():设置或返回元素的宽度(不包括内边距、边框或外边距)
  • height():设置或返回元素的高度(不包括内边距、边框或外边距)
  • innerwidth()方法返回元素的宽度(包括内边距)
  • innerheight()方法返回元素的高度(包括内边距)
  • outerwidth()方法返回元素的宽度(包括内边距和边框)
  • outerheight()方法返回元素的高度(包括内边距和边框)
  • outerwidth(true)方法返回元素的宽度(包括内边距、边框、外边距)
  • outerheight(true)方法返回元素的高度(包括内边距、边框、外边距)
<!doctype html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var txt="";
txt+="width of div: " + $("#div1").width() + "</br>";
txt+="height of div: " + $("#div1").height() + "</br>";
txt+="innerwidth of div: " + $("#div1").innerwidth() + "</br>";
txt+="innerheight of div: " + $("#div1").innerheight() + "</br>"
txt+="outer width: " + $("#div1").outerwidth() + "</br>"
txt+="outter height: " + $("#div1").outerheight() + "</br>";
txt+="outer width: " + $("#div1").outerwidth(true) + "</br>"
txt+="outter height: " + $("#div1").outerheight(true);
$("#div1").html(txt);
});
});
</script>
</head>
<body>
<div id="div1" style="height:150px;width:300px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;"></div>
<br>
<button>显示div尺寸</button>
</body>
</html>

运行结果:

jQuery实现获取及设置CSS样式操作详解

感兴趣的朋友可以使用在线html/css/javascript代码运行工具http://tools.jb51.net/code/htmljsrun测试上述代码运行效果。

更多关于jquery相关内容还可查看本站专题:《jquery切换特效与技巧总结》、《jquery扩展技巧总结》、《jquery常用插件及用法总结》、《jquery拖拽特效与技巧总结》、《jquery表格(table)操作技巧汇总》、《jquery常见经典特效汇总》、《jquery动画与特效用法总结》及《jquery选择器用法总结

希望本文所述对大家jquery程序设计有所帮助。