简体   繁体   English

动态显示/隐藏div与JavaScript无法正常工作

[英]Dynamic show/hide div with javascript not working

I have a basic show/hide javascript that works, as long as i don't make it dynamic and make sure of a parameter. 我有一个基本的显示/隐藏JavaScript工作,只要我不动态并确保参数。 I would appreciate a lot if anyone could help me figure out why the dynamic version doesn't work. 如果有人能帮助我弄清楚为什么动态版本不起作用,我会非常感激。

Working code: 工作代码:
javascript JavaScript的

function togglesDiv(){  
  var catdiv = document.getElementById("addNewCat");  
  if(catdiv.style.display == ""){  
    catdiv.style.display = "none";  
  } else {  
    catdiv.style.display = "";  
  }  
}  

html HTML

<span onclick="togglesDiv();">Add new category</span>  
<div id="addNewCat" style="display: none;">  
lalala  
</div>

Non working code: 非工作代码:
javascript JavaScript的

function togglesDiv(divsId){  
  var catdiv = document.getElementById("divsId");
  if(catdiv.style.display == ""){  
    catdiv.style.display = "none";  
  } else {  
    catdiv.style.display = "";  
  }  
}  

html HTML

<span onclick="togglesDiv(addNewCat);">Add new category</span>  
<div id="addNewCat" style="display: none;">  
lalala  
</div>

You have a variable name wrapped in string delimiters, making it a string literal instead of a variable. 您有一个变量名称包装在字符串分隔符中,使其成为字符串文字而不是变量。 Change 更改

var catdiv = document.getElementById("divsId");

To

var catdiv = document.getElementById(divsId);

On the flipside, the call to the function needs the quotes in it's argument (because it should be a string), you can use single quotes to avoid confliction: 另一方面,对函数的调用需要它的参数中的引号(因为它应该是一个字符串),你可以使用单引号来避免冲突:

<span onclick="togglesDiv('addNewCat');">Add new category</span>  

Remove the quotes: 删除引号:

var catdiv = document.getElementById("divsId");

Becomes

var catdiv = document.getElementById(divsId);

You don't have an element with an ID of "divsId". 您没有ID为“divsId”的元素。

On a completely unrelated note, you can't be sure that catdiv.style.display will always be equal to "" when it is visibile. 在一个完全不相关的注释中,当它是可见的时,你不能确定catdiv.style.display总是等于"" There are other styles which cause it to be displayed ('inline', 'block', for example). 还有其他样式可以显示它(例如'inline','block')。

A better solution might be: 更好的解决方案可能是:

function togglesDiv(divsId){  
  var catdiv = document.getElementById("divsId");
  if(catdiv.style.display === "none"){  
    catdiv.style.display = "";  
  } else {  
    catdiv.style.display = "none";  
  }  
}  

(And yes, I did mean to put the triple equals === in) (是的,我的意思是把三等于===

Your code is looking for a div with an ID "divsId" change your code to: 您的代码正在寻找ID为“divsId”的div将您的代码更改为:

   function togglesDiv(divsId){  
      var catdiv = document.getElementById(divsId);
      if(catdiv.style.display == ""){  
        catdiv.style.display = "none";  
      } else {  
        catdiv.style.display = "";  
      }  
    } 

Because you are looking for a div called "divsId" rather than the value in the variable divsId. 因为你正在寻找一个名为“divsId”的div而不是变量divsId中的值。 Remove the speach marks! 删除说明标记!

Remove quotes from var catdiv = document.getElementById("divsId"); var catdiv = document.getElementById("divsId");删除引号var catdiv = document.getElementById("divsId"); should be var catdiv = document.getElementById(divsId); 应该是var catdiv = document.getElementById(divsId);

And add quotes: 并添加引号:

<span onclick="togglesDiv(addNewCat);">Add new category</span>

Should be 应该

<span onclick="togglesDiv('addNewCat');">Add new category</span>  

Improved function 改进功能

function toggleDisplay(id){
    var el = document.getElementById(id);
    if(!el) return true;
    // feature detect to support IE versions.
    var dis = 'currentStyle' in el ? el.currentStyle.display : el.style.display;
    // toggle display
    el.style.display = /none/i.test(dis) ? '' : 'none';
    // prevent memory leak
    el = null;
}

And as mentioned, quotes are needed when writing yucky inline javascript. 如上所述,在编写令人讨厌的内联javascript时需要引号。

<span onclick="toggleDisplay('addNewCat')"> ... etc

Tbh. TBH。 pick a js toolkit/library and use it over reinventing the wheel yourself and stop writing inline javascript, your life and happiness will improve substantially if you do =P 选择一个js工具包/库并使用它自己重新发明轮子并停止编写内联javascript,如果你这样做,你的生活和幸福将大大改善

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

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