Javascript删除单个页面所有渲染页面中的table

发布于 2024-08-30 08:13:52 字数 603 浏览 2 评论 0原文

我有一个像这样的表格:

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

在很多页中。所有这些页面都呈现在单个页面中。当我应用 javascript 来删除该表时,使用以下内容加载:

var tbl = document.getElementById('toc');
if(tbl) tbl.parentNode.removeChild(tbl);

仅删除一个表,而不删除其他表。我正在尝试使用 javascript 删除所有渲染页面中的表格。如何做到这一点?

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

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

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 with the below:

var tbl = document.getElementById('toc');
if(tbl) tbl.parentNode.removeChild(tbl);

Only one table is deleted and not the others. I am trying to delete the tables in all the rendering pages using javascript. How to do this?

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> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2024-09-06 08:13:52

ID 是一个唯一标识符,这基本上意味着:只能有一个。

尝试通过标记名查找(table code>) 代替并比较类名。

var allTables = document.getElementsByTagName('table');

for (var i = 0; i < allTables.length; i++) {
    // Array.indexOf may not be available, see
    // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf

    if (allTables[i].className.split(' ').indexOf('toc') != -1) {
        var node = allTables[i];
        node.parentNode.removeChild(node);
    }
}

An ID is a unique identifier, which basically means: There can be only one.

Try looking up by tagname (table) instead and comparing the classname.

var allTables = document.getElementsByTagName('table');

for (var i = 0; i < allTables.length; i++) {
    // Array.indexOf may not be available, see
    // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf

    if (allTables[i].className.split(' ').indexOf('toc') != -1) {
        var node = allTables[i];
        node.parentNode.removeChild(node);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文