div的内容宽度可以根据用户要求使用clientWidth和scrollWidth属性动态获取。如果用户想知道实际显示内容所需的空间(包括填充所占用的空间,但不包括滚动条,边距或边框),则用户可以使用以下任何一种过程来返回整个内容的宽度。元素。
- 使用Element.ClientWidth属性
- 使用Element.scrollWidth属性
示例1:使用ClientWidth属性的div的Content Width将返回元素的整个内容的宽度。
GeeksforGeeks
Geeting Content width
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.
输出:
示例2:使用scrollWidth属性的div的Content Width将返回元素的整个内容的宽度。
GeeksforGeeks
Getting Content width
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.
输出: