简体   繁体   English

隐藏 div 的 Javascript 代码不起作用(CSS @media 规则也不起作用)-“无法读取 null 的属性样式”

[英]Javascript code hiding a div doesn't work (CSS @media rule doesn't work either)- 'cannot read property style of null'

So, I've got a button that display toggles a div on a click event.所以,我有一个buttondisplayclick事件上切换div It works properly.它工作正常。 However, I can't hide the same div using nearly the same code (however, I want to toggle this div after my screen becomes too big, not after clicking), because I get the problem like in the title- 'cannot read property style of null'但是,我无法使用几乎相同的代码隐藏相同的div (但是,我想在屏幕变得太大后切换此div ,而不是在单击后),因为我遇到了标题中的问题 - 'cannot read property null 样式

The part that doesn't work (hiding a div after screen becomes too big:不起作用的部分(在屏幕变得太大后隐藏一个div

if (screen.width > 900) {
  document.getElementById('klik').style.display = 'none';
}

And the part that works ( button toggles a div using a click event ):以及有效的部分( button使用click event切换div ):

function showDiv() {
  if (document.getElementById('klik').style.display == 'block'){
    document.getElementById('klik').style.display = 'none';
  }
  else{
    document.getElementById('klik').style.display = 'block';
  }
}

I wrote this code because I want to do a scalable menu, displaying a div with list items inside after clicking on it.我写这段代码是因为我想做一个可扩展的菜单,在点击它后显示一个带有列表项的div The menu button is visible only when screen-width <= 900px , if screen-width > 900px I've got a normal navigation bar and the button disappears.菜单button仅在screen-width <= 900px时可见,如果screen-width > 900px我有一个正常的导航栏并且button消失。

Am I forgetting something?我是不是忘记了什么? I'm new to Javascript.我是 Javascript 的新手。 Also one more thing- it also doesn't work using @media rule, however I can change the background-color with @media .还多了一个东西─它也不会使用工作@media规则,但是我可以改变background-color@media I hope it might help.我希望它会有所帮助。 Also thanks in advance.也提前致谢。

Note Two Problems:注意两个问题:

  1. Ensure that the element you want to change it's display has the id="klik"确保您要更改其display的元素具有id="klik"

  2. The below code will execute only once.下面的代码只会执行一次。

    if (screen.width > 900) { document.getElementById('klik').style.display = 'none'; }

    • But why?但为什么? The answer is because you didn't set an event to run it every time when your resize the screen.答案是因为您没有设置一个event来在每次resize屏幕resize时运行它。 Also, screen.width will always return the width of the display.此外, screen.width将始终返回显示的width What you are looking for is the window.innerWidth您正在寻找的是window.innerWidth

So a possible solution:所以一个可能的解决方案:

 window.addEventListener('resize', function(){ document.getElementById("screen").innerHTML = window.innerWidth.toString() + "px"; if(window.innerWidth < 900) { //Perform your desired Executions Here } });
 <div id="screen"></div>

This will run the code every time a window.onresize is triggered.这将在每次触发window.onresize时运行代码。 And that's exactly what @media in CSS does.而这正是 CSS 中的@media所做的。 It works on window.onresize behind the scene in javascript sense.它适用于 javascript 意义上的幕后window.onresize

Note: I have added a simple illustration on how to work with screen.resize which you can use as the basis for modifying element properties based on a certain range.注意:我添加了一个关于如何使用screen.resize的简单说明,您可以将其用作基于特定范围修改元素属性的基础。 All you need to do, is to ensure that you do your styling within that block and it will work.您需要做的就是确保您在该块内进行样式设置并且它会起作用。

Hmmm, actually this is exactly what media queries are made for.嗯,实际上这正是媒体查询的目的。 Did you try你试过了吗

@media (min-width: 900px) {
  #klik {
    display:  none;
  }
}

If that doesn't work: Are there any other css styles that may overwrite that particular style?如果这不起作用:是否还有其他 css 样式可能会覆盖该特定样式? Something like #klik { display: block !important;类似 #klik { display: block !important; } ...? } ...?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM