在获得窗口大小后,如何在页面加载时使用Javascript更改HTML ID?

在获得窗口大小后,如何在页面加载时使用Javascript更改HTML ID?,javascript,html,navigation,Javascript,Html,Navigation,我想知道如何使用Javascript获得一个windows大小,然后用它设置一个HTML ID(非常清晰) 我目前正在一个网站上工作,该网站的“默认”导航ID为“access”,如果windows大小小于800 px,我想将其删除 这是我正在处理的页面,如果清除ID,我可以设置移动导航以使用引导 `window.onload=函数(){ var w=window.innerWidth; 如果(w

我想知道如何使用Javascript获得一个windows大小,然后用它设置一个HTML ID(非常清晰)

我目前正在一个网站上工作,该网站的“默认”导航ID为“access”,如果windows大小小于800 px,我想将其删除

这是我正在处理的页面,如果清除ID,我可以设置移动导航以使用引导

`window.onload=函数(){

var w=window.innerWidth;
如果(w<800){
document.getElementById('access')。removeAttribute('id');
}
}

window.onload=函数(){

var w=window.innerWidth;
如果(w<800){
document.getElementById('access')。removeAttribute('id');
}
}

window.onresize=函数(){

var w=window.innerWidth;
如果(w<800){
document.getElementById('access')。removeAttribute('id');
}
}使用

var w = window.innerWidth;
获取页面的宽度

然后呢,

window.onload = function() {
    if(w < 800) {
        document.getElementById('access').removeAttribute('id');
    }
}

另外,在我看来,添加/删除
id
不是一个好主意。

您的代码必须在问题中。你不能链接到实时站点供我们调试。为什么不使用CSS媒体查询?不建议设置和取消设置id,请使用其他方法跟踪。我也将使用媒体查询,但此id正与其他Javascript函数一起用于在悬停时显示菜单。嗨,Pratyush,感谢您的回复,您的回复非常有效。我意识到我还需要在调整窗口大小时清除它,这样,如果它低于800,就可以清除。如果用户重新调整窗口的大小大于800?有没有设置空白ID的方法?code>window.onload=function(){var w=window.innerWidth;if(w<800){document.getElementById('access').removeAttribute('id');}}}window.onresize=function(){var w=window.innerWidth;if(w<800){document.getElementById('access').removeAttribute('id');}}
var w = window.innerWidth;

if(w < 800) {
    document.getElementById('access').removeAttribute('id');
}
var w = window.innerWidth;
window.onload = function() {
    if(w < 800) {
        document.getElementById('access').removeAttribute('id');
    }
}
window.onload = function() {
    var accessElem = document.getElementById('access');
    var updateAccess = function() { 
        var w = window.innerWidth;
        if(w < 800) {
            accessElem.removeAttribute('id');
        }
        else {
            accessElem.setAttribute('id', 'access');
        }
    };
    window.onresize = updateAccess;    // call updateAccess on resize
    updateAccess();                    // call once on load to set it.
};