jQuery innerWidth()
jQuery innerWidth()方法用于返回第一个匹配的内部宽度
元素,不包括边框和边距。
此方法包括填充,但不包括边框和边距。
此图说明jQuery innerWidth()方法包括填充,但不包括边框和边距。
语法 :
jQuery innerWidth()方法示例
让我们以一个示例来演示jQuery innerWidth()方法。
<!DOCTYPE html> <html> <head> <script src="/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ alert("Inner width of div is: " + $("div").innerWidth()); }); }); </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 inner width of the div</button> </body> </html>
输出:
Click here to get the inner width of the div
jQuery innerWidth()示例2
让我们以一个示例来演示jQuery innerWidth()方法的效果。
<!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).innerWidth(); $("#result").html("Inner 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>
输出: