在这段代码中
function adjustStyle() {
var width = 0;
// get the width.. more cross-browser issues
if (window.innerHeight) {
width = window.innerWidth;
} else if (document.documentElement && document.documentElement.clientHeight) {
width = document.documentElement.clientWidth;
} else if (document.body) {
width = document.body.clientWidth;
}
// now we should have it
if (width < 600) {
document.getElementById("myCSS").setAttribute("href", "_css/narrow.css");
} else {
document.getElementById("myCSS").setAttribute("href", "_css/main.css");
}
}
// now call it when the window is resized.
window.onresize = function () {
adjustStyle();
};为什么我必须在空函数中使用adjustStyle(),为什么我不能像window.onresize =adjustStyle()那样使用;
发布于 2017-10-30 17:54:37
您可以调用:
window.onresize = adjustStyle;像这样声明adjustStyle:
var adjustStyle = function() { [...] };或
function adjustStyle() { [...] }发布于 2017-10-30 17:56:51
为什么我必须在空函数中使用adjustStyle(),为什么我不能像window.onresize =adjustStyle()那样使用;
因为如果您这样做了,onresize将被分配调用函数的结果-这是未定义的-因此不会发生任何事情。但是,您可以这样做:
window.onresize = adjustStyle; // note no parentheses这会将resize设置为函数本身。
发布于 2017-10-30 17:56:21
因为使用一个函数,您可以在调整大小后调用多个处理程序。
例如:
window.onresize = function(){
doOneThing();
doAnotherThing();
}https://stackoverflow.com/questions/47012281
复制相似问题