首页 > 解决方案 > 选择表格中的填充单元格

问题描述

如果单元格已填充,我正在尝试使其可编辑。如果尚未填写,则无法编辑。

我正在使用 contenteditable 但我无法选择 td 的所有内容都适合我的条件。当我尝试写 if 时,当一个 td 被填满时,所有 td 都被选中。我试图在 foreach 内部写 if 但我想我不能这样做,因为 jquery selection 不返回任何数组等。

你有什么建议吗?

var edit3 = $('#table_mresults tbody tr').find(':nth-child(3)');
var edit4 = $('#table_mresults tbody tr').find(':nth-child(4)');

if(edit3 != "" && edit4 != ""){


    edit3.attr('contenteditable','true');
    edit4.attr('contenteditable','true');
}

var edit3 = $('#table tbody tr').find(':nth-child(3)');
var edit4 = $('#table tbody tr').find(':nth-child(4)');

edit3.forEach(e3 => {
    if(e3 != "") e3.attr('contenteditable','true');
});

edit4.forEach(e4 => {
    if(e4 != "") e4.attr('contenteditable','true');
});

编辑:解决方案建议

也许我应该尝试为填充的行提供可编辑的类,然后用这个类名选择?

标签: javascriptjqueryselectioncontenteditable

解决方案


尝试检查元素内部文本而不是元素本身,如下所示:

if(edit3.text().trim() != "" && edit4.text().trim() != ""){


    edit3.attr('contenteditable','true');
    edit4.attr('contenteditable','true');
}


推荐阅读