javascript 根据浏览器窗口大小动态更改主体 ID(使用 css) [英] javascript change body ID (with css) dynamically with respect to browser window size

查看:19
本文介绍了javascript 根据浏览器窗口大小动态更改主体 ID(使用 css)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我所拥有的,但它不起作用:

Here is what I have, but it is not working:

 if(window.width() < 1000)
document.getElementsByTagName('body')[0].id="be"
else
document.getElementsByTagName('body')[0].id="bd"
;

请告诉我我写错了什么.请不要告诉其他解决方案,请修复此代码.

Please tell me what I am writing wrong. Please don't tell other solutions, please just fix this code.

基本上,如果浏览器窗口大小小于 1000px,我想为网站的 BODY 标签提供不同的 ID.

Basically, I want to give a different ID to the BODY tag of a website if the browser window size is less than 1000px.

推荐答案

给你:

document.body.id = window.innerWidth < 1000 ? 'be' : 'bd';

<小时>

将此代码放在页面底部:


Place this code at the bottom of your page:

<script>
    function setBodyId() {
        document.body.id = window.innerWidth < 1000 ? 'be' : 'bd';
    }           
    window.onload = setBodyId;
    window.onresize = setBodyId;
</script>

现场演示: http://jsfiddle.net/simevidas/THqXB/

这篇关于javascript 根据浏览器窗口大小动态更改主体 ID(使用 css)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆