【开发小技巧】018—如何使用AngularJS动态获取div的内容宽度?

英文 | https://www.geeksforgeeks.org/how-to-dynamically-get-the-content-width-of-a-div-using-angularjs/?ref=rp

div的内容宽度可以根据用户要求使用clientWidth和scrollWidth属性动态获取。如果用户想知道实际显示内容所需的空间(包括填充所占用的空间,但不包括滚动条,边距或边框),则用户可以使用以下任何过程将返回内容的整个内容的宽度。元件。

  • 使用Element.ClientWidth属性

  • 使用Element.scrollWidth属性

示例1:使用ClientWidth属性的div的Content Width将返回元素整个内容的宽度。

<!DOCTYPE html> <html>   <head>     <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> </script>     <style>         #div1 {             height: 100px;             width: 300px;             border: 2px solid black;             overflow: scroll;         }                   h1 {             color: green;         } </style> </head>   <body>     <center>         <h1>GeeksforGeeks</h1>         <h3>Geeting Content width</h3>         <div id="div1">           Calculate content width of a div on GeeksforGeek.           GeeksforGeeks is a computer science portal which            helps students to learn various programming language           and master data structures and algorithms. There are           various courses available to learn new skills.         </div>         <br>         <button onclick="contentwidth()">Content Width of Div</button>           <p id="p1"></p>     </center>     <script>         function contentwidth() {             var ans = "Content-width: "             + angular.element(document.getElementById("div1").clientWidth)             + "px<br>";             document.getElementById("p1").innerHTML = ans;         } </script> </body>   </html> 

最终效果如下:

示例2:使用scrollWidth属性的div的Content Width将返回元素的整个内容的宽度。

<!DOCTYPE html> <html>   <head>     <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> </script>     <style>         #div1 {             height: 100px;             width: 300px;             border: 2px solid black;             overflow: scroll;         }                   h1 {             color: green;         } </style> </head>   <body>     <center>         <h1>GeeksforGeeks</h1>         <h3>Getting Content width</h3>         <div id="div1">           Calculate content width of a div on GeeksforGeek.            GeeksforGeeks is a computer science portal which            helps students to learn various programming language           and master data structures and algorithms. There are           various courses available to learn new skills.         </div>         <br>         <button onclick="contentwidth()">Content Width of Div</button>           <p id="p1"></p>     </center>     <script>         function contentwidth() {             var ans = "Content-width: "              + angular.element(document.getElementById("div1").scrollWidth)              + "px<br>";             document.getElementById("p1").innerHTML = ans;         } </script> </body>   </html> 

最终效果截图如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值