如何使用javascript通过id隐藏html div

时间:2022-11-25 18:10:34

I am trying to hide an HTML div with Javascript and am getting an error

我试图用Javascript隐藏HTML div并且收到错误

JavaScript runtime error: Unable to get property 'style' of undefined or null reference

JavaScript运行时错误:无法获取未定义或空引用的属性“样式”

Here is my JavaScript code:

这是我的JavaScript代码:

if (typeof(jsondata[0]) == 'undefined') {
    alert('should be hidden');
    document.getElementById("unassignedDevices").style.visibility = "hidden";
}

and here is my HTML:

这是我的HTML:

<div id="unassignedDevices">
    <button id="unassignedDevicesbutton" class="button-basicmenu" onclick="findUnassignedDevices();">Find unassigned Devices</button>

    <table id="gridunassignedDevices"></table>

</div>

So when I start debugging, the page alerts 'should be hidden' and then I get the JavaScript runtime error.

因此,当我开始调试时,页面警报'应该被隐藏'然后我得到JavaScript运行时错误。

How can I hide this div?

我怎么能隐藏这个div?

2 个解决方案

#1


$(document).ready(function(){
   if (typeof (jsondata[0]) == 'undefined') {
      $('#unassignedDevices').hide();
    }
});

#2


Javascript is faster than jQuery a little bit, but why cant you used jQuery instead of pure javascript alone ?

Javascript比jQuery快一点,但为什么你不能单独使用jQuery而不是纯javascript?

hir is sample code for the hide option. sample 1:

hir是hide选项的示例代码。样本1:

 $(document).ready(function(){
         $('#id').hide();
     });

sample 2:

$(function(){
   var index = {
      init: function(){
        //all jQuery calls
        $(document).on('click', '#id', this.exe_this_func);
      },
      exe_this_func: function(e){
          $(this).hide();
          e.preventDefault();
      }
   }
   index.init();
})();

#1


$(document).ready(function(){
   if (typeof (jsondata[0]) == 'undefined') {
      $('#unassignedDevices').hide();
    }
});

#2


Javascript is faster than jQuery a little bit, but why cant you used jQuery instead of pure javascript alone ?

Javascript比jQuery快一点,但为什么你不能单独使用jQuery而不是纯javascript?

hir is sample code for the hide option. sample 1:

hir是hide选项的示例代码。样本1:

 $(document).ready(function(){
         $('#id').hide();
     });

sample 2:

$(function(){
   var index = {
      init: function(){
        //all jQuery calls
        $(document).on('click', '#id', this.exe_this_func);
      },
      exe_this_func: function(e){
          $(this).hide();
          e.preventDefault();
      }
   }
   index.init();
})();