1.效果图
2.源码
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <style type="text/css"> div{ border:1px solid black; width:200px; height: 95px; resize:both; overflow: auto; } </style> <script type="text/javascript"> function showSize(elementSize) { console.log(""+elementSize.width+";height:"+elementSize.height); } HTMLDivElement.prototype.onsizechange = function (handleFunction) { var element = this; var lastWidth = element.clientWidth; var lastHeight = element.clientHeight; setInterval(function () { if (lastWidth === element.clientWidth && lastHeight === element.clientHeight) return; if (typeof (handleFunction) == 'function') { lastWidth = element.clientWidth; lastHeight = element.clientHeight; handleFunction({ lastWidth, height: lastHeight }); } }, 100); return element; }; window.onload = function () { var myDiv = document.getElementById("myDiv"); myDiv.onsizechange(showSize); }; </script> </head> <body> <div id="myDiv"><p>尺寸可变元素<br/>200px;height:95px</p></div> </body> </html>