You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除删除表格内容后残留的空白<tr>行?

如何移除删除表格内容后残留的空白行?

嘿,我太懂你这个烦恼了!你之前的脚本应该是只把表格行里的内容删掉了,但没把整个表格行(<tr>元素)本身从DOM里移除,那些绿色的空块就是被掏空了内容但还赖在那的空<tr>~

给你两个解决思路,直接就能搞定:

  • 从根源解决:直接删除整个目标行
    别再只删行里的内容啦,找到包含"scary""disturbing""macabre"这些描述符的单元格后,直接定位它的父节点<tr>,然后把整个行删掉。比如你可以把脚本改成这样:

    // ==UserScript==
    // @name         RYM 移除指定描述符行
    // @namespace    http://tampermonkey.net/
    // @version      0.1
    // @description  移除RYM投票页中带指定描述符的表格行
    // @match        *://rateyourmusic.com/*voting*
    // @grant        none
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        const targetDescriptors = ['scary', 'disturbing', 'macabre'];
        // 找到所有描述符对应的单元格(这里的选择器要根据RYM实际页面结构调整,你可以按F12看元素class)
        const descriptorCells = document.querySelectorAll('td[class*="descriptor"]');
    
        descriptorCells.forEach(cell => {
            const text = cell.textContent.trim().toLowerCase();
            if (targetDescriptors.includes(text)) {
                // 找到当前单元格所在的整个表格行
                const row = cell.closest('tr');
                if (row) row.remove(); // 直接删掉整个行,不留空壳
            }
        });
    })();
    
  • 补漏方案:清理已存在的空行
    如果你的旧脚本已经删了内容,或者页面上还有其他原因产生的空行,可以加一段代码遍历所有表格行,把空的都删掉:

    // 遍历页面上的表格行,删除空行
    document.querySelectorAll('table[class*="voting-table"] tr').forEach(row => {
        // 检查行内是否没有任何可见内容
        const hasVisibleContent = Array.from(row.children).some(td => {
            return td.textContent.trim() !== '' || td.children.length > 0;
        });
        if (!hasVisibleContent) {
            row.remove();
        }
    });
    

小提醒:记得用浏览器的F12开发者工具,查看RYM页面上表格和描述符单元格的实际class,把脚本里的选择器替换成准确的,这样脚本才能精准定位到目标元素哦~

备注:内容来源于stack exchange,提问作者aldin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 17:19:55