获取隐藏元素个数

$('.timeLine>div').filter(function(){ return $(this).css('display')==='none';}).length;

非常有效,希望能给予同伴帮助!

<think>我们正在讨论如何使用JavaScript获取HTML元素的计算样式。根据引用[2]和引用[3],我们可以使用`window.getComputedStyle()`方法来获取元素计算后的样式(即最终应用在元素上的所有样式,包括浏览器默认样式、外部样式表、内部样式表和行内样式)。对于旧版IE(IE8及以下),我们可以使用`currentStyle`属性。 ### 步骤: 1. **获取元素**:首先通过JavaScript获取目标DOM元素。 2. **判断浏览器支持情况**: - 现代浏览器:使用`window.getComputedStyle(element)`。 - 旧版IE:使用`element.currentStyle`。 3. **获取具体样式属性**:通过上述方法返回的对象,我们可以通过属性名(字符串)获取具体的样式值。 ### 示例代码: 以下是一个跨浏览器的获取元素计算样式的函数: ```javascript function getComputedStyleValue(element, propertyName) { // 现代浏览器 if (window.getComputedStyle) { return window.getComputedStyle(element, null).getPropertyValue(propertyName); } // 旧版IE else if (element.currentStyle) { // 注意:IE的currentStyle属性使用驼峰命名,需要转换属性名 // 例如:将'background-color'转换为'backgroundColor' var camelCaseProp = propertyName.replace(/\-(\w)/g, function (match, letter) { return letter.toUpperCase(); }); return element.currentStyle[camelCaseProp]; } // 如果都不支持,返回空字符串或内联样式(作为备选) else { return element.style[propertyName]; } } ``` ### 使用示例: 假设我们有一个元素: ```html <div id="myDiv" style="width: 100px;">Hello World</div> ``` 我们可以这样获取它的宽度: ```javascript var myDiv = document.getElementById('myDiv'); var width = getComputedStyleValue(myDiv, 'width'); console.log(width); // 输出 "100px" ``` ### 注意事项: 1. **属性名**:在`getComputedStyle`方法中,属性名可以是CSS属性名(如`'background-color'`),而IE的`currentStyle`则需要驼峰命名(如`'backgroundColor'`)。因此,在函数中我们做了转换。 2. **返回值的单位**:获取的样式值通常带有单位(如`px`、`%`等),但某些属性(如`z-index`)返回的是数字。 3. **伪元素**:`getComputedStyle`还可以用于获取元素的样式,第二个参数传入伪元素名称,如`':before'`。 4. **性能**:频繁调用`getComputedStyle`可能会导致性能问题,因为每次调用都会重新计算样式。 ### 引用说明: - 根据引用[2],我们使用了`window.getComputedStyle()`来获取计算后的样式[^2]。 - 引用[3]提供了一个兼容IE的示例,我们参考了其中的思路[^3]。 ### 相关问题: 1. 如何获取隐藏元素(`display: none`)的计算样式? 2. `getComputedStyle` 和 `element.style` 有什么区别? 3. 如何获取元素(如 `::before`)的样式? [^2]: JavaScript学习笔记(十三)——获取元素计算后样式 [^3]: JavaScript零基础入门 9:JavaScript读取元素的样式</think># JavaScript 获取 HTML 元素计算样式 在 JavaScript 中,获取 HTML 元素的计算样式(即最终渲染到页面上的样式)有多种方法,下面我将详细介绍最常用的几种方式: ## 1. 使用 `window.getComputedStyle()` 这是现代浏览器中获取元素计算样式的标准方法: ```javascript // 获取元素 const element = document.getElementById('myElement'); // 获取计算样式 const computedStyle = window.getComputedStyle(element); // 获取具体样式值 const width = computedStyle.width; const backgroundColor = computedStyle.backgroundColor; console.log(`元素宽度: ${width}, 背景色: ${backgroundColor}`); ``` ### 获取元素样式 ```javascript // 获取 ::before 伪元素的样式 const beforeStyle = window.getComputedStyle(element, '::before'); const content = beforeStyle.content; ``` ## 2. IE 浏览器的兼容方案 (IE8及以下) 对于旧版 IE 浏览器,需要使用 `currentStyle` 属性: ```javascript function getComputedStyleValue(element, property) { if (window.getComputedStyle) { // 现代浏览器 return window.getComputedStyle(element).getPropertyValue(property); } else if (element.currentStyle) { // IE8及以下 // 注意:IE 使用驼峰命名法 const camelCaseProp = property.replace(/-(\w)/g, (_, letter) => letter.toUpperCase()); return element.currentStyle[camelCaseProp]; } else { // 最终回退方案 return element.style[property]; } } // 使用示例 const color = getComputedStyleValue(element, 'color'); ``` ## 3. 获取元素尺寸的特殊方法 对于元素尺寸,可以使用更直接的属性: ```javascript const element = document.getElementById('myElement'); // 获取元素内容宽度(包括内边距) const clientWidth = element.clientWidth; // 获取元素总宽度(包括内边距、边框) const offsetWidth = element.offsetWidth; // 获取元素位置 const rect = element.getBoundingClientRect(); console.log(`元素位置: 上 ${rect.top}px, 左 ${rect.left}px`); ``` ## 4. 样式获取的注意事项 1. **单位问题**:获取的样式值通常包含单位(如 `px`、`%`) ```javascript const width = window.getComputedStyle(element).width; // "100px" ``` 2. **颜色格式**:颜色值可能以不同格式返回(RGB、HEX 等) ```javascript const color = window.getComputedStyle(element).color; // "rgb(255, 0, 0)" ``` 3. **隐藏元素**:对于 `display: none` 的元素,某些尺寸属性返回 0 ```javascript // 当元素隐藏时 element.offsetWidth; // 0 element.offsetHeight; // 0 ``` ## 完整示例 ```html <!DOCTYPE html> <html> <head> <style> #sample { width: 200px; height: 100px; background-color: #3498db; border: 2px solid #2c3e50; margin: 20px; padding: 15px; } </style> </head> <body> <div id="sample">样式示例元素</div> <button onclick="showStyles()">获取样式</button> <div id="result" style="margin-top: 20px; padding: 10px; background: #f8f9fa;"></div> <script> function showStyles() { const element = document.getElementById('sample'); const resultDiv = document.getElementById('result'); // 获取计算样式 const computedStyle = window.getComputedStyle(element); // 获取元素位置信息 const rect = element.getBoundingClientRect(); // 构建结果显示 let html = ` <h3>元素计算样式:</h3> <p>宽度: ${computedStyle.width}</p> <p>高度: ${computedStyle.height}</p> <p>背景色: ${computedStyle.backgroundColor}</p> <p>边框: ${computedStyle.border}</p> <p>内边距: ${computedStyle.padding}</p> <p>外边距: ${computedStyle.margin}</p> <h3>尺寸信息:</h3> <p>clientWidth: ${element.clientWidth}px</p> <p>offsetWidth: ${element.offsetWidth}px</p> <p>位置: 上 ${rect.top}px, 左 ${rect.left}px</p> `; resultDiv.innerHTML = html; } </script> </body> </html> ``` ## 关键点总结 1. **`window.getComputedStyle()`** 是现代浏览器中获取计算样式的最佳方法 2. **`element.currentStyle`** 用于兼容旧版 IE 浏览器 3. **`clientWidth`/`clientHeight`** 获取内容区域尺寸(包括内边距) 4. **`offsetWidth`/`offsetHeight`** 获取元素整体尺寸(包括内边距和边框) 5. **`getBoundingClientRect()`** 获取元素位置和精确尺寸 通过组合使用这些方法,您可以全面获取元素的最终渲染样式信息[^2][^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值