innerWidth()function is used to set or return the inner width of the currently matched element .
The inner width includes the inner margin (padding) of the element, but does not include the height of the margins (margin), border (border), and so on. Such as:
If you want to get the width of other cases, please use width () and outerwidth (), you can click here to see the difference between the three.
The function belongs to the jQuery object (instance) and is still valid for elements that are not visible.
Grammar
JQuery 1.2.6 adds this function.
Jqueryobject. Innerwidth([])
Attention:
1. If the parameter is omitted value , the inner width is obtained, and if the parameter is specified, the inner width is set.
2 innerWidth() . The function's "set" action is for each element that the current jquery object matches, and the read operation is only for the first matched element.
Parameters
| Parameters |
Description |
| Value |
The optional/number type is used to set the inner width value. |
The innerWidth() inner width value cannot be set until 1.8.0 is used only to get the inner width (no parameters are accepted).
JQuery 1.8.0 New support: innerWidth() function support parameters value for setting the inner width of each matching element.
Parameter value can also be a function, the innerWidth() function will be executed according to all the elements of the match , and the this corresponding DOM element will be pointed to in the function.
innerWidth()Two parameters are also passed to the function: The first parameter is the index of the current element in the matching element, and the second parameter is the current inner width of the element. The return value of the function is the inner width that needs to be set.
return value
innerWidth()The return value of the function is the jquery/number type, and the type of the return value depends on whether the innerWidth() function is currently performing a "set" or "read" operation.
If the innerWidth() function performs a set operation, the current jquery object itself is returned, and if it is a read operation, the inner width of the first matching element is returned.
If the current jquery object matches more than one element, the function returns the inner width, innerWidth() whichever is the first one of the matching elements. If there are no matching elements, it is returned null .
innerWidth()Not applicable window and document , please use width () instead.
Example & Description
Take the following HTML code as an example:
<div Id="N1" Style="Padding: 10px;Width:100px;Height: 100px;Border: 1pxSolid #000; "></div>
<div id= "N2" style= "margin :10px; Width: 150px; Height: 100px; Background: #999; ></DIV>
The following jquery sample code demonstrates innerWidth() the specific use of a function:
Var$n 1=$("#n1");
Var$n 2=$("#n2");
Document.Writeln($n 1.Innerwidth() ); 120
Document.Writeln($n 2.Innerwidth() ); 150
Var$divs=$("Div");
If multiple elements are matched, only the innerwidth of the first element is returned
Document.Writeln($divs.Innerwidth() ); 120
/* * * * * * * * * The following jquery code is limited to 1.8.0 and later * * *
Set the innerwidth of all DIV elements to be less than 150px (less than 150 is set to 150, others remain unchanged)
$divs.Innerwidth( function (index, Innerwidth) {
return math. Max (innerwidth, 150< Span class= "pun"); } );
//settings n1 innerwidth for 20px
$n 1. 20
Jquery.innerwidth () function in detail