juqery 学习之六 CSS--css、位置、宽高 - Web前端
作者:98wpeu发布时间:2026-08-04分类:网页前端技术浏览:177
导读:CSS(name)访问第一个匹配元素的样式属性。----------------------------------------------------------------...
CSS(name)
访问第一个匹配元素的样式属性。
--------------------------------------------------------------------------------
return a style property on the first matched element.
返回值
string
参数
name (String) : 要访问的属性名称
示例
取得第一个段落的color样式属性的值。
JQuery 代码:
$("p").css("color");
-------------------------------------------------------------------------------------------------------------------------------------------------
css(PRoperties)
把一个“名/值对”对象设置为所有匹配元素的样式属性。
这是一种在所有匹配的元素上设置大量样式属性的最佳方式。
--------------------------------------------------------------------------------
Set a key/valueObject as style propertIEs to all matched elements.
this is the best way to set several style properties on all matched elements.
返回值
jQuery
参数
properties (map) : 要设置为样式属性的名/值对
示例
将所有段落的字体颜色设为红色并且背景为蓝色。
jquery 代码:
$("p").css({ color: "#ff0011", background: "blue" });
--------------------------------------------------------------------------------
如果属性名包含 "-"的话,必须使用引号:
jquery 代码:
$("p").css({ "margin-left": "10px", "background-color": "blue" });
-------------------------------------------------------------------------------------------------------------------------------------------------
css(name,value)
在所有匹配的元素中,设置一个样式属性的值。
数字将自动转化为像素值
--------------------------------------------------------------------------------
Set a single style property to a value on all matched elements.
If a number is provided, it is automatically converted into a pixel value.
返回值
JQuery
参数
name (value) : 属性名
value (String, Number) : 属性值
示例
将所有段落字体设为红色
jQuery 代码:
$("p").css("color","red");
-------------------------------------------------------------------------------------------------------------------------------------------------
offset()
获取匹配元素在当前视口的相对偏移。
返回的对象包含两个整形属性:top 和 left。此方法只对可见元素有效。
--------------------------------------------------------------------------------
get the current offset of the first matched element relative to the viewport.
The returned object contAIns two Integer properties, top and left. The method works only with visible elements.
返回值
Object{top,left}
示例
获取第二段的偏移
html 代码:
<p>Hello</p><p>2nd Paragraph</p>
jQuery 代码:
var p = $("p:last");
var offset = p.offset();
p.HTML( "left: " + offset.left + ", top: " + offset.top );
结果:
<p>Hello</p><p>left: 0, top: 35</p>
-------------------------------------------------------------------------------------------------------------------------------------------------
height()
取得第一个匹配元素当前计算的高度值(px)。
在 jQuery 1.2 以后可以用来获取 window 和 document 的高
--------------------------------------------------------------------------------
Get the current computed, pixel, height of the first matched element.
In jQuery 1.2, this method is able to find the height of the window and document.
返回值
Integer
示例
获取第一段的高
jQuery 代码:
$("p").height();
--------------------------------------------------------------------------------
获取文档的高
jQuery 代码:
$(document).height();
把所有段落的高设为 20:
jQuery 代码:
$("p").height(20);
-------------------------------------------------------------------------------------------------------------------------------------------------
width()
取得第一个匹配元素当前计算的宽度值(px)。
在 jQuery 1.2 以后可以用来获取 window 和 document 的宽
--------------------------------------------------------------------------------
Get the current computed, pixel, width of the first matched element.
In jQuery 1.2, this method is able to find the width of the window and document.
返回值
Integer
示例
获取第一段的宽
jQuery 代码:
$("p").width();
--------------------------------------------------------------------------------
获取当前窗口的宽
jQuery 代码:
$(window).width();
将所有段落的宽设为 20:
jQuery 代码:
$("p").width(20);
访问第一个匹配元素的样式属性。
--------------------------------------------------------------------------------
return a style property on the first matched element.
返回值
string
参数
name (String) : 要访问的属性名称
示例
取得第一个段落的color样式属性的值。
JQuery 代码:
$("p").css("color");
-------------------------------------------------------------------------------------------------------------------------------------------------
css(PRoperties)
把一个“名/值对”对象设置为所有匹配元素的样式属性。
这是一种在所有匹配的元素上设置大量样式属性的最佳方式。
--------------------------------------------------------------------------------
Set a key/valueObject as style propertIEs to all matched elements.
this is the best way to set several style properties on all matched elements.
返回值
jQuery
参数
properties (map) : 要设置为样式属性的名/值对
示例
将所有段落的字体颜色设为红色并且背景为蓝色。
jquery 代码:
$("p").css({ color: "#ff0011", background: "blue" });
--------------------------------------------------------------------------------
如果属性名包含 "-"的话,必须使用引号:
jquery 代码:
$("p").css({ "margin-left": "10px", "background-color": "blue" });
-------------------------------------------------------------------------------------------------------------------------------------------------
css(name,value)
在所有匹配的元素中,设置一个样式属性的值。
数字将自动转化为像素值
--------------------------------------------------------------------------------
Set a single style property to a value on all matched elements.
If a number is provided, it is automatically converted into a pixel value.
返回值
JQuery
参数
name (value) : 属性名
value (String, Number) : 属性值
示例
将所有段落字体设为红色
jQuery 代码:
$("p").css("color","red");
-------------------------------------------------------------------------------------------------------------------------------------------------
offset()
获取匹配元素在当前视口的相对偏移。
返回的对象包含两个整形属性:top 和 left。此方法只对可见元素有效。
--------------------------------------------------------------------------------
get the current offset of the first matched element relative to the viewport.
The returned object contAIns two Integer properties, top and left. The method works only with visible elements.
返回值
Object{top,left}
示例
获取第二段的偏移
html 代码:
<p>Hello</p><p>2nd Paragraph</p>
jQuery 代码:
var p = $("p:last");
var offset = p.offset();
p.HTML( "left: " + offset.left + ", top: " + offset.top );
结果:
<p>Hello</p><p>left: 0, top: 35</p>
-------------------------------------------------------------------------------------------------------------------------------------------------
height()
取得第一个匹配元素当前计算的高度值(px)。
在 jQuery 1.2 以后可以用来获取 window 和 document 的高
--------------------------------------------------------------------------------
Get the current computed, pixel, height of the first matched element.
In jQuery 1.2, this method is able to find the height of the window and document.
返回值
Integer
示例
获取第一段的高
jQuery 代码:
$("p").height();
--------------------------------------------------------------------------------
获取文档的高
jQuery 代码:
$(document).height();
把所有段落的高设为 20:
jQuery 代码:
$("p").height(20);
-------------------------------------------------------------------------------------------------------------------------------------------------
width()
取得第一个匹配元素当前计算的宽度值(px)。
在 jQuery 1.2 以后可以用来获取 window 和 document 的宽
--------------------------------------------------------------------------------
Get the current computed, pixel, width of the first matched element.
In jQuery 1.2, this method is able to find the width of the window and document.
返回值
Integer
示例
获取第一段的宽
jQuery 代码:
$("p").width();
--------------------------------------------------------------------------------
获取当前窗口的宽
jQuery 代码:
$(window).width();
将所有段落的宽设为 20:
jQuery 代码:
$("p").width(20);
相关推荐
- WordPress 应用:如何修改模版头部文件中的 wp_head() - Web前端
- SEO 优化:YouTube SEO Plugin 提升影片流量 - Web前端
- 使用 React 服务器组件在 Next.js 13 中进行国际化 - Web前端
- 3 个意想不到的技术 SEO 问题示例(以及如何解决它们) - Web前端
- Vue 技术专题:Suspense、路由模式与单元测试实践 - Web前端
- 如何防止 WordPress SQL 注入攻击? - Web前端
- SEO 优化:如何围绕技术SEO框架以构建和制定策略? - Web前端
- SEO优化:YouTube SEO 关键词研究工具 - Web前端
- 2025 年帮助代理机构提升客户业绩的 14 款最佳 SEO 工具 - Web前端
- 客户端:服务器之间设置本地 DNS 服务器 - Web前端
- 网页前端技术排行
-
- 1[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 2分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 3jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 4【第六章】Foundation之按钮和下拉功能 - Web前端
- 5基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 6jQuery实现鼠标移到元素上动态提示消息框效果 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 9jQuery编写widget的一些技巧分享 - Web前端
- 最近发表
-
- WordPress 应用:如何修改模版头部文件中的 wp_head() - Web前端
- SEO 优化:YouTube SEO Plugin 提升影片流量 - Web前端
- 使用 React 服务器组件在 Next.js 13 中进行国际化 - Web前端
- 3 个意想不到的技术 SEO 问题示例(以及如何解决它们) - Web前端
- Vue 技术专题:Suspense、路由模式与单元测试实践 - Web前端
- 如何防止 WordPress SQL 注入攻击? - Web前端
- SEO 优化:如何围绕技术SEO框架以构建和制定策略? - Web前端
- SEO优化:YouTube SEO 关键词研究工具 - Web前端
- 2025 年帮助代理机构提升客户业绩的 14 款最佳 SEO 工具 - Web前端
- 客户端:服务器之间设置本地 DNS 服务器 - Web前端


