使用js如何设置、获取盒模型的宽和高

第一种:

dom.style.width/height

这种方法只能获取使用内联样式的元素的宽和高。

第二种:

dom.currentStyle.width/height

这种方法获取的是浏览器渲染以后的元素的宽和高,无论是用何种方式引入的css样式都可以,但只有IE浏览器支持这种写法。

第三种:

window.getComputedStyle(dom).width/height

这种方法获取的也是浏览器渲染以后的元素的宽和高,但这种写法兼容性更好一些。

第四种:

dom.getBoundingClientRect().width/height

这种方法经常使用的场所是,计算一个元素的绝对位置(相对于视窗左上角),它能拿到元素的left、top、width、height 4个属性。

原文地址:https://www.cnblogs.com/yangdaren/p/10755653.html

时间: 2024-08-01 23:07:34

使用js如何设置、获取盒模型的宽和高的相关文章

JS设置和获取盒模型的宽和高

dom.style.width/height:只能取出内联样式的宽度和高度 dom.currentStyle.width/height:获取即时的计算的样式,但是只有IE支持 window.getComputedStyle(dom).width:获取即时计算的样式,支持其他浏览器,兼容性更好 dom.getBoundingClientRect( ).width/height:计算盒模型在页面中的绝对位置,比较少用. dom.offsetWidth/offectHeight:返回元素实际大小 一.

JS如何设置和获取盒模型对应的宽和高

㈠方式一:通过DOM节点的 style 样式获取  dom.style.width/height  只能获取使用内联样式的元素的宽和高. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>>js获取盒模型宽和高的方法</title> <style> *{ margin: 0; paddin

【Android】获取控件的宽和高

有时候我们须要在Activity的时候获取控件的宽和高来做一些操作,以下介绍三种获取宽和高的方式: 1. onWindowFocusChanged @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { int width = image.getMeasuredWidth(); int height = image.ge

JS获取盒模型对应的宽高

## 获取内联样式宽高 只能获取内联设置的样式,在style或者.css文件中设置的无法获取 1 let div = document.querySelect('.test'); 2 let width = div.style.width 3 let height = div.style.height ## currentStyle和getComputedStyle获取所有样式 两者只能获取样式,不能设置样式 let div = document.querySelect('.test'); le

对于盒模型的宽高获取填坑

最近,在做一个项目的时候,要获取一个盒模型的宽度,那么当时想到的方法就是el.style.width.我们先看一段代码. 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>juke</title> 5 <style type="text/css"> 6 #box { 7 width: 300px; 8 height: 300px; 9 border: 1px solid #0

(前端)html与css,css 8、盒模型、宽高、盒模型内减

盒模型 概述 盒模型:也叫框模型 一个盒子内容包含了五部分:宽度.高度.内边距.边框.外边距. 代码↓ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"

js封装设置获取cookie

var docCookies = { getItem: function (sKey) { return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), &qu

ImageMagick:获取一行文字的宽与高

double *fm = MagickQueryFontMetrics(mw_temp, dw_wand, text_utf8); //获取文字在指定字体和字号下的宽度和高度 double textWidth = fm[11]; //文本的宽度, 不 +1 右侧有些字母少一个像素不能显示 /** textHeight = fm[2] - fm[3] , 其实大部分情况下: fm[2](ascender) - fm[3](descender) = fm[6](maximum horizontal

css盒模型

css盒子模型是为了让我们充分理解div+css模型的定位功能,盒子模型在学习div+css布局方式中必须要学习的一个模型. 那什么是css盒模型呢? 网页设计中常听到的属性名:内容(content).填充(padding).边框(border).边界(margin).css盒模型都具备这些属性.这些属性和日常生活中盒子的属性是一样的.内容就是盒子里面装的东西,而填充就是像盒子里装的一些反震的材料,边框就是箱子的本身,边界呢就像盒子之间的空隙. 盒子的模型有2种,分别是IE盒子模型和标准盒子模型