Sự khác biệt giữa
3 và
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
4 là giá trị sau trả về giá trị pixel không có đơn vị [ví dụ:
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
5] trong khi giá trị trước trả về giá trị còn nguyên đơn vị [ví dụ:
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
6]. Phương pháp
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
4 được khuyến nghị khi chiều rộng của phần tử cần được sử dụng trong phép tính toán học
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
Phương pháp này cũng có thể tìm thấy chiều rộng của cửa sổ và tài liệu
1
2
3
4
5
// Returns width of browser viewport
$[ window ].width[];
// Returns width of HTML document
$[ document ].width[];
Lưu ý rằng
4 sẽ luôn trả về chiều rộng nội dung, bất kể giá trị của thuộc tính CSS
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
9 là gì. Kể từ jQuery 1. 8, điều này có thể yêu cầu truy xuất thuộc tính chiều rộng CSS cộng với thuộc tính
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
9 và sau đó trừ đi bất kỳ đường viền và phần đệm tiềm năng nào trên mỗi phần tử khi phần tử đó có
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
1. Để tránh hình phạt này, hãy sử dụng
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
3 thay vì
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
4
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
Ghi chú. Mặc dù các thẻ
4 và
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
5 sẽ báo cáo giá trị cho
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
4 hoặc
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
7 khi được định vị tuyệt đối và được cung cấp
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
8, nhưng chúng tôi đặc biệt khuyên bạn nên gọi các phương thức đó trên các thẻ này. Ngoài việc là một thông lệ xấu, kết quả cũng có thể không đáng tin cậy
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
Ghi chú bổ sung
- Số được trả về bởi các API liên quan đến thứ nguyên, bao gồm cả
4, có thể là phân số trong một số trường hợp. Mã không nên cho rằng nó là một số nguyên. Ngoài ra, các kích thước có thể không chính xác khi người dùng thu phóng trang;width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
- Giá trị được báo cáo bởi
4 không đảm bảo chính xác khi phần tử hoặc phần tử gốc của nó bị ẩn. Để có được giá trị chính xác, hãy đảm bảo phần tử hiển thị trước khi sử dụngwidth demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
4. jQuery sẽ cố gắng hiển thị tạm thời và sau đó ẩn lại một phần tử để đo kích thước của phần tử đó, nhưng điều này không đáng tin cậy và [ngay cả khi chính xác] có thể ảnh hưởng đáng kể đến hiệu suất của trang. Tính năng đo hiển thị và ẩn này có thể bị xóa trong phiên bản tương lai của jQuerywidth demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
Ví dụ
Hiển thị các chiều rộng khác nhau. Lưu ý rằng các giá trị từ iframe nên có thể nhỏ hơn bạn mong đợi. Phần tô sáng màu vàng hiển thị nội dung khung nội tuyến
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
Thử nghiệm
.width[ value ]Trả về. jQuery
Sự miêu tả. Đặt chiều rộng CSS của từng phần tử trong tập hợp các phần tử phù hợp
đã thêm phiên bản. 1. 0 . chiều rộng[giá trị]
giá trị
Loại. Chuỗi hoặc Số
Một số nguyên biểu thị số lượng pixel hoặc một số nguyên cùng với một đơn vị đo lường tùy chọn được thêm vào [dưới dạng một chuỗi]
đã thêm phiên bản. 1. 4. 1 . chiều rộng[ chức năng ]
chức năng
Loại. Hàm [Chỉ số số nguyên, Giá trị số nguyên] => Chuỗi hoặc Số
Một hàm trả về chiều rộng để đặt. Nhận vị trí chỉ mục của phần tử trong tập hợp và chiều rộng cũ làm đối số. Trong hàm,
2 đề cập đến phần tử hiện tại trong tập hợpwidth demo
div {
width: 70px;
height: 50px;
float: left;
margin: 5px;
background: red;
cursor: pointer;
}
.mod {
background: blue;
cursor: default;
}
d
d
d
d
d
var modWidth = 50;
$[ "div" ].one[ "click", function[] {
$[ this ].width[ modWidth ].addClass[ "mod" ];
modWidth -= 8;
}];
Khi gọi
3, giá trị có thể là chuỗi [số và đơn vị] hoặc số. Nếu chỉ một số được cung cấp cho giá trị, jQuery sẽ giả định một đơn vị pixel. Tuy nhiên, nếu một chuỗi được cung cấp, thì bất kỳ phép đo CSS hợp lệ nào cũng có thể được sử dụng cho chiều rộng [chẳng hạn như
width demo
div {
width: 70px;
height: 50px;
float: left;
margin: 5px;
background: red;
cursor: pointer;
}
.mod {
background: blue;
cursor: default;
}
d
d
d
d
d
var modWidth = 50;
$[ "div" ].one[ "click", function[] {
$[ this ].width[ modWidth ].addClass[ "mod" ];
modWidth -= 8;
}];
4,
width demo
div {
width: 70px;
height: 50px;
float: left;
margin: 5px;
background: red;
cursor: pointer;
}
.mod {
background: blue;
cursor: default;
}
d
d
d
d
d
var modWidth = 50;
$[ "div" ].one[ "click", function[] {
$[ this ].width[ modWidth ].addClass[ "mod" ];
modWidth -= 8;
}];
5 hoặc
width demo
div {
width: 70px;
height: 50px;
float: left;
margin: 5px;
background: red;
cursor: pointer;
}
.mod {
background: blue;
cursor: default;
}
d
d
d
d
d
var modWidth = 50;
$[ "div" ].one[ "click", function[] {
$[ this ].width[ modWidth ].addClass[ "mod" ];
modWidth -= 8;
}];
6]. Lưu ý rằng trong các trình duyệt hiện đại, thuộc tính chiều rộng CSS không bao gồm phần đệm, đường viền hoặc lề, trừ khi thuộc tính CSS
width demo
div {
width: 70px;
height: 50px;
float: left;
margin: 5px;
background: red;
cursor: pointer;
}
.mod {
background: blue;
cursor: default;
}
d
d
d
d
d
var modWidth = 50;
$[ "div" ].one[ "click", function[] {
$[ this ].width[ modWidth ].addClass[ "mod" ];
modWidth -= 8;
}];
9 được sử dụng
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];
Nếu không có đơn vị rõ ràng nào được chỉ định [như "em" hoặc "%"] thì "px" được giả định
Lưu ý rằng
3 đặt chiều rộng nội dung của hộp bất kể giá trị của thuộc tính CSS
width demo
div {
width: 70px;
height: 50px;
float: left;
margin: 5px;
background: red;
cursor: pointer;
}
.mod {
background: blue;
cursor: default;
}
d
d
d
d
d
var modWidth = 50;
$[ "div" ].one[ "click", function[] {
$[ this ].width[ modWidth ].addClass[ "mod" ];
modWidth -= 8;
}];
9
width demo
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
Get Paragraph Width
Get Document Width
Get Window Width
Sample paragraph to test width
function showWidth[ ele, w ] {
$[ "div" ].text[ "The width for the " + ele + " is " + w + "px." ];
}
$[ "#getp" ].click[function[] {
showWidth[ "paragraph", $[ "p" ].width[] ];
}];
$[ "#getd" ].click[function[] {
showWidth[ "document", $[ document ].width[] ];
}];
$["#getw"].click[function[] {
showWidth[ "window", $[ window ].width[] ];
}];