jquery width() method


  Translation results:

width

UK [wɪdθ] US [wɪdθ, wɪθ, wɪtθ]

n.Width; breadth

jquery width() methodsyntax

Function: width() method returns or sets the width of the matching element.

Return width: Return the width of the first matching element. If no parameters are set for this method, the width of the matched element in pixels is returned.

Syntax: $(selector).width()

Set the width: Set the width of all matching elements.

Syntax: $(selector).width(length)

##Parameters:

ParameterDescriptionlength Optional. Specifies the width of the element. If no length unit is specified, the default px unit is used.

#Use a function to set the width: Use a function to set the width of all matching elements.

Syntax: $(selector).width(function(index,oldwidth))

Parameters:

ParametersDescriptionfunction(index,oldwidth) Specifies to return the new width of the selected element function. index Optional. Accepts the index position of the selector oldvalue Optional. Accepts the current value of the selector.

jquery width() methodexample

<html>
<head>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
    $("p").width(200);
  });
});
</script>
</head>
<body>
<p style="background-color:yellow">This is a paragraph.</p>
<button class="btn1">改变宽度</button>
</body>
</html>
Run instance »

Click the "Run instance" button to view the online instance