jQuery中outerWidth()方法用法实例

方法获取第一个匹配元素外部宽度。
默认情况下外部高度是宽度(width)、内补白(padding)和边框(border)之和。
如果参数设置为true的话,外补白(margin)尺寸也会算入外部宽度。
方法对可见和隐藏元素均有效。
可以与outerHeight()方法结合学习。

语法结构:

复制代码 代码如下:

$(selector).outerWidth(options)


参数列表:
参数   描述  
options   定义是否把外补白(margin)计算在内:
一.fase,外补白不计算在内,默认值。
二.true,外补白计算在内。
 

实例代码:

复制代码 代码如下:


<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta content="https://www.jb51.net/" />
<title>outerWidth()函数-脚本之家</title>
<style type="text/css">
div{
  background-color:green;
  height:100px;
  width:200px;
  padding:10px;
  margin:10px;
  border:5px solid red;
}  
</style>
<script type="text/javascript" src="https://www.jb51.net/mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("div").text($("div").outerWidth(true))
  })
})
</script>
</head>
<body>
  <div>此处显outerWidth()数值</div>
  <button>点击查看div的outerWidth</button>
</body>
</html>

点击按钮可以显示div的元素外部宽度。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgxwjg.html