事件处理- Javascript addEventListener不读取resize



嗨,我试图添加一个eventListener,它添加了一个h1标签的宽度和浏览器的高度,当你调整它的大小。我不明白为什么它不工作。

<!DOCTYPE html>
<html>
<head>
	<title>Window size</title>
</head>
<body>
<div id="wh"></div>
<script type="text/javascript">
"use strict";
document.addEventListener("onresize", function(){
	var width = document.documentElement.clientWidth;
	var height = document.documentElement.clientHeight;
	document.getElementById('wh').innerHTML = "<h1>Width: " + width + "  Height: " + height + "</h1>";
});
</script>
</body>
</html>

你可以使用

window.onresize = function(event) {
    var width = document.documentElement.clientWidth;
    var height = document.documentElement.clientHeight;
    document.getElementById('wh').innerHTML = "<h1>Width: " + width + "  Height: " + height + "</h1>";
};

代替document。addEventListener,否则就是事件"resize"而不是"onresize"

您有错误的事件,它需要简单地'调整'。更多示例参见这里- http://www.w3schools.com/jsref/event_onresize.asp

<!DOCTYPE html>
<html>
<head>
	<title>Window size</title>
</head>
<body>
<div id="wh"></div>
<script type="text/javascript">
"use strict";
window.addEventListener("resize", function(){
	var width = document.documentElement.clientWidth;
	var height = document.documentElement.clientHeight;
	document.getElementById('wh').innerHTML = "<h1>Width: " + width + "  Height: " + height + "</h1>";
});
</script>
</body>
</html>

相关内容

  • 没有找到相关文章

最新更新