<template> <div id="topInfo" ref="topInfo" style="width: 200px;height: 200px;background-color: #0bb20c"> <img id="imgInfo" ref="imgInfo" src="./20151205231902_xe2kG.jpeg" alt="" height="200px" > </div> </template> <script> export default { name: "center", mounted() { //div 標簽獲取的方法 let topInfowidth = this.$refs.topInfo.style.width; let topInfoheight = this.$refs.topInfo.style.height; console.log("topInfo:"+topInfowidth+" "+topInfoheight) console.log("=================================================") //img 標簽獲取的方法 let imgInfowidth = this.$refs.imgInfo.width; let imgInfoheight = this.$refs.imgInfo.height; console.log("imgInfo:"+imgInfowidth+" "+imgInfoheight) let src = this.$refs.imgInfo.src; console.log("src:"+src) } //獲取dom元素高度通過在標簽里面定義ref屬性,用this.$refs.自定義名稱.offsetHight;去獲取。 } </script> <style scoped> #topInfo{ overflow: hidden; } </style>
注意:需要在mounted中才能獲取到
