DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> juqery 學習之六 CSS--css、位置、寬高
juqery 學習之六 CSS--css、位置、寬高
編輯:JQuery特效代碼     
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);
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved