irpas技术客

vue获取页面dom元素样式的方法_晚风_vue获取dom元素样式

大大的周 7875

一、$refs

给dom元素加ref=“name”

在js中

this.$refs.name.style.width

就能获取到该dom元素的width

二、document.querySelector('DOM元素').style

document.querySelector('.dingwei').style.height

三、styleSheets样式表

styleSheets能获取页面所有的dom节点的样式

document.styleSheets[3].cssRules[3].style.width

四、JavaScript原生方法

getRealStyle(obj, styleName) { // Js获取元素样式【支持内联】 var realStyle = null; if (obj.currentStyle) { realStyle = obj.currentStyle[styleName]; } else if (window.getComputedStyle) { realStyle = window.getComputedStyle(obj, null)[styleName]; } return realStyle; },

页面直接调用,obj是dom节点,styleName是属性名

const divs = document.querySelector("dom节点"); this.getRealStyle(divs, "height");

五、

const divs = document.querySelector("dom节点"); let div = window.getComputedStyle(divs, null).width console.log(div)


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #vue获取dom元素样式