Javascript 使用Javascript删除从不同页面呈现的整个表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2688602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:28:23  来源:igfitidea点击:

Delete the entire table rendered from different pages using Javascript

javascripthtml-table

提问by useranon

I am having a table like:

我有一张像:

 <table id="toc" class="toc" border="1" summary="Contents">
 </table>

in many pages .. All these pages are rendered in a single page. when I apply the Javascript to delete that using on load. Only one table is deleted and not the others.

在许多页面中......所有这些页面都呈现在一个页面中。当我应用 Javascript 在加载时删除它时。仅删除一张表,而不删除其他表。

I am trying to delete the tables in all the rendering pages using Javascript. How to do this?

我正在尝试使用 Javascript 删除所有渲染页面中的表格。这该怎么做?

Edit :
I myself found the solution

编辑:
我自己找到了解决方案

 <script type='text/javascript'>
    window.onLoad = load(); 
  function load(){var tbl = document.getElementById('toc'); 
   if(tbl) tbl.parentNode.removeChild(tbl);} 
 </script> 

回答by Sarfraz

Really want to delete the table altogether?

真的要彻底删除表吗?

var elem = documenet.getElementById('toc');

if (typeof elem != 'undefined')
{
  elem.parentNode.removeChild(elem);
}

You could also hidethe table rather than deleting it.

您也可以隐藏表格而不是删除它。

var elem = documenet.getElementById('toc');
elem.style.display = 'none';

If you need it later, you could simply do:

如果您以后需要它,您可以简单地执行以下操作:

var elem = documenet.getElementById('toc');
elem.style.display = 'block';

回答by Jonathan

Here's a rough sample

这是一个粗略的样本

<html>
<head>
<script type="text/javascript">

    function removeTable(id)
    {
        var tbl = document.getElementById(id);
        if(tbl) tbl.parentNode.removeChild(tbl);
    }

</script>
</head>
<body>

<table id="toc" class="toc" border="1" summary="Contents">
    <tr><td>This table is going</td></tr>
</table>

<input type="button" onclick="removeTable('toc');" value="Remove!" />

</body>
</html>

回答by Salil

<script type="text/javascript">
  function deleteTable(){
    document.getElementById('div_table').innerHTML="TABLE DELETED"
  }
</script>

<div id="div_table">

 <table id="toc" class="toc" border="1" summary="Contents">
 </table>
</div>
<input type="button" onClick="deleteTable()">

回答by Serhiy

var tbl = document.getElementById(id);
tbl.remove();