juqery 学习之六 CSS--css、位置、宽高

 更新时间:2011年02月11日 02:02:46   作者:  
juqery 学习之六 CSS--css、位置、宽高,需要的朋友可以参考下。
css(name)
访问第一个匹配元素的样式属性。

--------------------------------------------------------------------------------

Return a style property on the first matched element.
返回值
String

参数
name (String) : 要访问的属性名称

示例
取得第一个段落的color样式属性的值。

jQuery 代码:

$("p").css("color");
-------------------------------------------------------------------------------------------------------------------------------------------------
css(properties)
把一个“名/值对”对象设置为所有匹配元素的样式属性。
这是一种在所有匹配的元素上设置大量样式属性的最佳方式。

--------------------------------------------------------------------------------

Set a key/value object 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);
您可能感兴趣的文章:

相关文章

  • jQuery利用cookie 实现本地收藏功能(不重复无需多次命名)

    jQuery利用cookie 实现本地收藏功能(不重复无需多次命名)

    cookie 是存储于访问者计算机中的变量。这篇文章主要介绍了jQuery利用cookie 实现本地收藏功能不重复无需多次命名,需要的朋友可以参考下
    2019-11-11
  • jQuery-mobile事件监听与用法详解

    jQuery-mobile事件监听与用法详解

    这篇文章主要介绍了jQuery-mobile事件监听与用法,结合实例形式分析了jQuery-mobile各种常见事件类型与使用技巧,需要的朋友可以参考下
    2016-11-11
  • jquery ajax post提交数据乱码

    jquery ajax post提交数据乱码

    今天发现在使用jquery ajax.post提交数据时会发现数据在ff正常,但在chrome与ie浏览器中post过去的数据全部是乱码
    2013-11-11
  • jQuery中:first-child选择器用法实例

    jQuery中:first-child选择器用法实例

    这篇文章主要介绍了jQuery中:first-child选择器用法,实例分析了:first-child选择器的功能、定义及匹配父元素的第一个子元素的用法技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2014-12-12
  • jQuery实现简单弹出框效果实例

    jQuery实现简单弹出框效果实例

    这篇文章主要给大家介绍了关于jQuery实现简单弹出框效果的相关资料,几天一直在研究JQuery,确实很好用,有很多需求都是要弹出框,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-06-06
  • jQuery实现订单提交页发送短信功能前端处理方法

    jQuery实现订单提交页发送短信功能前端处理方法

    这篇文章主要介绍了jQuery实现订单提交页发送短信功能前端处理方法,涉及jQuery响应鼠标事件及针对页面元素的样式与字符串正则操作相关技巧,需要的朋友可以参考下
    2016-07-07
  • jquery模拟picker实现滑动选择效果

    jquery模拟picker实现滑动选择效果

    这篇文章主要为大家详细介绍了jquery模拟picker实现滑动选择效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • SuperSlide标签切换、焦点图多种组合插件

    SuperSlide标签切换、焦点图多种组合插件

    SuperSlide 是致力于实现网站统一特效调用的函数,能解决大部分标签切换、焦点图切换等效果,还能多个slide组合创造更多的效果。(兼容ie内核(包括无敌的 ie6)、webkit内核、ff、opera等主流浏览器)。
    2015-03-03
  • jquery图片不完全按比例自动缩小的简单代码

    jquery图片不完全按比例自动缩小的简单代码

    开发前期用自动生产 固定大小的图片 进行 显示,发现不能满足,在前期的时候把保存了原图,现在只能显示原图,原图由于上传时候没有做任何限制所有要使用 图片不完全按比例缩小,下面是代码
    2013-07-07
  • jQuery实现伸展与合拢panel的方法

    jQuery实现伸展与合拢panel的方法

    这篇文章主要介绍了jQuery实现伸展与合拢panel的方法,可实现操作div层的平滑收缩与展开的功能,涉及jQuery中next、slideUp、slideDown等方法的使用技巧,非常简单实用,需要的朋友可以参考下
    2015-04-04

最新评论