width()方法获取第一个匹配元素的当前计算像素宽度。
selector.width( )
此方法能够找到窗口和文档的宽度,如下所示:
$(window).width(); //返回浏览器窗口的宽度 $(document).width(); //返回 HTML 文档的宽度
<html> <head> <title>The jQuery Example</title> <script type="text/javascript" src="https://ajax.googleapis.com/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).height(); $("#result").html("That div is <span>" +color+ "</span>."); $("#result").css({'color': color, 'background-color':'gray'}); $("#result").width( width ); }); }); </script> <style> div { width:60px; height:60px; margin:5px; float:left; } </style> </head> <body> <p>Click on any square:</p> <span id="result"> </span> <div style="background-color:blue; height:50px;"></div> <div style="background-color:pink;height:30px;"></div> <div style="background-color:#123456;height:100px;"></div> <div style="background-color:#f11; height:75px;"></div> </body> </html>
这将产生以下输出-
链接:https://www.learnfk.com/jquery/css-width.html
来源:LearnFk无涯教程网
这一章《jQuery - CSS - width( )方法 函数》你学到了什么?在下面做个笔记吧!做站不易,你的分享是对我们最大的支持
使用 Webpack 将 jQuery 公开给真实的 Window 对象
如何在使用 jQuery 单击特定链接时打开 bootstrap 导航选项卡的...