jQuery externalWidth()
jQuery outsideWidth()方法用于返回第一个匹配的外部宽度
jQuery outsideWidth()方法适用于可见和隐藏元素。
在上图中,您可以看到jQuery outsideWidth()方法同时包含border和padding。
语法 :
$(selector).outerWidth(includeMargin)
jQuery的参数externalWidth()方法
参数
说明
includeMargin
这是一个可选参数。它是一个布尔值,它指定是否包括边距。 false: 是默认值。它指定不包括边距。 true: 它指定包括边距的内容。
jQuery的outsideWidth()方法示例
让我们以一个示例来演示jQuery outsideWidth()方法的效果。
<!DOCTYPE html> <html> <head> <script src="/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ alert("Outer width of div is: " + $("div").outerWidth()); }); }); </script> </head> <body> <div style="height:100px;width:500px;padding:10px;margin:3px;border:1px solid blue;background-color:lightpink;"></div><br> <button>Click here to get the outer width of the div</button> </body> </html>
输出:
Click here to get the outer width of the div
jQuery outsideWidth()示例2
<!DOCTYPE html> <html> <head> <title>The jQuery Example</title> <script type="text/javascript" src="/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $("div").click(function () { var color = $(this).css("background-color"); var width = $(this).outerWidth( true ); $("#result").html("Outer Width is <span>" + width + "</span>."); $("#result").css({'color': color, 'background-color':'white'}); }); }); </script> <style> #div1{ margin:10px;padding:10px; border:2px solid #666; width:60px;} #div2 { margin:15px;padding:15px; border:4px solid #666; width:60px;} #div3 { margin:20px;padding:20px; border:6px solid #666; width:60px;} #div4 { margin:25px;padding:25px; border:8px solid #666; width:60px;} </style> </head> <body> <p>Click on any square:</p> <span id="result"> </span> <div id="div1" style="background-color:orange;"></div> <div id="div2" style="background-color:green;"></div> <div id="div3" style="background-color:brown;"></div> <div id="div4" style="background-color:violet;"></div> </body> </html>
输出: