javascript - jQuery .width()、style.width 和 jQuery .css ('width' 之间有什么区别?

标签 javascript jquery

jQuery 的 .width()、jQuery 的 .css('width').style 返回的值有什么区别.width 节点上的属性?

我很好奇,因为在尝试将两个表格单元格的宽度设置为相等时,前两个给了我同样的错误答案。它们实际测量的是什么,为什么它与我在浏览器中查看元素时在 .style 属性中看到的值不同?

最佳答案

来自 width 官方文档

The difference between .css(width) and .width() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example, 400px). The .width() method is recommended when an element's width needs to be used in a mathematical calculation.

我会说 .css('width') 之间没有区别和 .style.with作为css()方法用于设置样式对象属性。

.css('width')之间的区别/.width().style.with是 jQuery 方法使用 window.getComputedStyle( elem, null ) 获取计算样式.此方法用于在应用 CSS 和样式后读取实际属性。

关于javascript - jQuery .width()、style.width 和 jQuery .css ('width' 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17494906/

相关文章:

javascript - 如何在node.style中使用div类样式元素?

javascript - Chrome : JavaScript window. 打开可保存

javascript - 使用 `.toggle()` 而不是应用类来显示内容

javascript - 结合 javascript 和 jquery 来填充多个隐藏字段 - 坏主意?

javascript - 再次加载 html 对象后加载 jQuery 函数

javascript - HTML5 响应式导航问题

jquery - 单击按钮时禁用 Tooltipster 工具提示?

javascript - 为 Datetimepicker Bundle 设置回调

用于验证电子邮件地址的 JavaScript/Jquery RegEx 无法正常工作

javascript - 如何更改 Google map v3 方向上虚线的颜色