jQuery CSS操作
来源:互联网 发布:阿里云修改登录密码 编辑:程序博客网 时间:2024/05/25 05:37
1、css() 设置或返回匹配元素的样式属性
(1)、返回 CSS 属性值
返回第一个匹配元素的 CSS 属性值。注释:当用于返回一个值时,不支持简写的 CSS 属性(比如 "background" 和 "border")。
语法:$(selector).css(name)
name必需。规定 CSS 属性的名称。该参数可包含任何 CSS 属性。比如 "color"。
实例:取得第一个段落的 color 样式属性的值
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ alert($("p").css("color")); });});</script></head><body><p style="color:red">This is a paragraph.</p><button type="button">返回段落的颜色</button></body></html>
(2)、设置 CSS 属性
设置所有匹配元素的指定 CSS 属性。
语法:$(selector).css(name,value)
name必需。规定 CSS 属性的名称。该参数可包含任何 CSS 属性,比如 "color"。
value可选。规定 CSS 属性的值。该参数可包含任何 CSS 属性值,比如 "red"。如果设置了空字符串值,则从元素中删除指定属性。
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ $("p").css("color","red"); });});</script></head><body><p>This is a paragraph.</p><p>This is another paragraph.</p><button type="button">改变段落的颜色</button></body></html>
(3)、使用函数来设置 CSS 属性
设置所有匹配的元素中样式属性的值。此函数返回要设置的属性值。接受两个参数,index 为元素在对象集合中的索引位置,value 是原先的属性值。
语法:$(selector).css(name,function(index,value))
name必需。规定 CSS 属性的名称。该参数可包含任何 CSS 属性,比如 "color"。
function(index,value) 规定返回 CSS 属性新值的函数。
index - 可选。接受选择器的 index 位置
oldvalue - 可选。接受 CSS 属性的当前值。
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("div").click(function() { $(this).css( "width", function(index, value) {return parseFloat(value) *3;} ); });});</script><style>div {width:100px; height:50px; background-color:red;}</style></head><body><div>请点击这里</div></body></html>
(4)、设置多个 CSS 属性/值对
语法:$(selector).css({property:value, property:value, ...})
把“名/值对”对象设置为所有匹配元素的样式属性。这是一种在所有匹配的元素上设置大量样式属性的最佳方式。
{property:value} 必需。规定要设置为样式属性的“名称/值对”对象。该参数可包含若干对 CSS 属性名称/值。比如 {"color":"red","font-weight":"bold"}
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ $("p").css({ "color":"white", "background-color":"#98bf21", "font-family":"Arial", "font-size":"20px", "padding":"5px", "width":"300px" }); });});</script></head><body><p>This is a paragraph.</p><p>This is another paragraph.</p><button type="button">改变段落的样式</button></body></html>
2、offset()返回第一个匹配元素相对于文档的位置
offset() 方法返回或设置匹配元素相对于文档的偏移(位置)。
(1)、返回偏移坐标
返回第一个匹配元素的偏移坐标。该方法返回的对象包含两个整型属性:top 和 left,以像素计。此方法只对可见元素有效。
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ x=$("p").offset(); $("#span1").text(x.left); $("#span2").text(x.top); });});</script></head><body><p>本段落的偏移是 <span id="span1">unknown</span> left 和 <span id="span2">unknown</span> top。</p><button>获得 offset</button></body></html>
(2)、设置偏移坐标
设置所有匹配元素的偏移坐标。
语法:$(selector).offset(value)
value必需。规定以像素计的 top 和 left 坐标。可能的值:值对,比如 {top:100,left:0}、带有 top 和 left 属性的对象
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ $("p").offset({top:100,left:10}); });});</script></head><body><p>This is a paragraph.</p><button>设置新的偏移</button></body></html>
(3)、使用函数来设置偏移坐标
使用函数来设置所有匹配元素的偏移坐标。
语法:$(selector).offset(function(index,oldoffset))
function(index,oldoffset) 规定返回被选元素新偏移坐标的函数。
index - 可选。接受选择器的 index 位置
oldvalue - 可选。接受选择器的当前坐标。
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ $("p").offset(function(n,c){ newPos=new Object(); newPos.left=c.left+100; newPos.top=c.top+100; return newPos; }); });});</script></head><body><p>这是一个段落。</p><button>设置 p 元素的 offset 坐标</button></body></html>
3、position()返回第一个匹配元素相对于父元素的位置
position() 方法返回匹配元素相对于父元素的位置(偏移)。该方法返回的对象包含两个整型属性:top 和 left,以像素计。此方法只对可见元素有效。
语法:$(selector).position()
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ x=$("p").position(); alert("Left position: " + x.left + " Top position: " + x.top); });});</script></head><body><p>This is a paragraph.</p><button>获得 p 元素的位置坐标</button></body></html>
4、width() 设置或返回匹配元素的宽度
(1)返回宽度
返回第一个匹配元素的宽度。如果不为该方法设置参数,则返回以像素计的匹配元素的宽度。
语法:$(selector).width()
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $(".btn1").click(function(){ $("span").text($("p").width()); });});</script></head><body><p>本段落的宽度是 <span>unknown</span> px。</p><button class="btn1">获得宽度</button></body></html>
(2)、设置宽度
设置所有匹配元素的宽度。
语法:$(selector).width(length)
length可选。规定元素的宽度。如果没有规定长度单位,则使用默认的 px 单位。
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $(".btn1").click(function(){ $("p").width(200); });});</script></head><body><p style="background-color:yellow">This is a paragraph.</p><button class="btn1">改变宽度</button></body></html>
(3)、使用函数来设置宽度
使用函数来设置所有匹配元素的宽度。
语法:$(selector).width(function(index,oldwidth))
function(index,oldwidth) 规定返回被选元素新宽度的函数。
index - 可选。接受选择器的 index 位置
oldvalue - 可选。接受选择器的当前值。
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ $("p").width(function(n,c){ return c-50; }); });});</script></head><body><p style="background-color:yellow">这是一个段落。</p><button>以 50 像素的幅度减少 p 元素的宽度</button></body></html>
5、height() 设置或返回匹配元素的高度
(1)、返回高度
返回第一个匹配元素的高度。如果不为该方法设置参数,则返回以像素计的匹配元素的高度。
语法:$(selector).height()
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $(".btn1").click(function(){ $("span").text($("p").height()); });});</script></head><body><p>本段落的高度是 <span>unknown</span> px。</p><button class="btn1">获得高度</button></body></html>
(2)、设置高度
设置所有匹配元素的高度。
语法:$(selector).height(length)
length可选。规定元素的高度。如果没有规定长度单位,则使用默认的 px 单位。
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $(".btn1").click(function(){ $("p").height(50); });});</script></head><body><p style="background-color:yellow">This is a paragraph.</p><button class="btn1">改变高度</button></body></html>
(3)、使用函数来设置高度
使用函数来设置所有匹配元素的高度。
语法:$(selector).height(function(index,oldheight))
function(index,oldheight) 规定返回被选元素新高度的函数。
index - 可选。接受选择器的 index 位置
oldvalue - 可选。接受选择器的当前值。
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ $("p").height(function(n,c){ return c+10; }); });});</script></head><body><p style="background-color:yellow">这是一个段落.</p><button>以 10 像素的幅度增加 p 元素的高度</button></body></html>
- jQuery CSS 操作 - css() 方法
- jQuery CSS 操作 - css() 方法
- jQuery CSS 操作 - css() 方法
- Jquery CSS 操作
- jQuery CSS 操作函数
- jQuery CSS 操作函数
- jquery操作CSS样式
- jQuery操作css样式
- jquery对CSS操作
- jQuery 参考手册 - CSS 操作
- jquery css操作
- jQuery CSS 操作
- jquery的css操作
- Jquery 使用手册------CSS操作
- jQuery 参考手册 - CSS 操作
- 使用jQuery操作CSS
- jQuery中的CSS操作
- JQuery 操作css样式
- Learning Python 008 正则表达式-004 sub()方法
- 基于XML配置的SpringMVC项目示例
- android源码解析系列
- #Paper Reading# Nonnegative Sparse PCA
- HTML基础学习之 --网页的宽度、高度自适应
- jQuery CSS操作
- POJ 1050最大子矩阵和
- myeclipse修改编码大全
- NSString的各种修改方法
- android产品研发系列
- 常用串口调试工具比较
- SVN代码管理遇到的坑
- POJ 1091 跳蚤(容斥原理)
- 修改PDF.NET SOD源代码以支持加密的数据库连接字符串