javascript - 选择表格中的填充单元格
问题描述
如果单元格已填充,我正在尝试使其可编辑。如果尚未填写,则无法编辑。
我正在使用 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');
});
编辑:解决方案建议
也许我应该尝试为填充的行提供可编辑的类,然后用这个类名选择?
解决方案
尝试检查元素内部文本而不是元素本身,如下所示:
if(edit3.text().trim() != "" && edit4.text().trim() != ""){
edit3.attr('contenteditable','true');
edit4.attr('contenteditable','true');
}
推荐阅读
- mysql - vagrant vm中docker中mysql卷容器的问题
- javascript - 直接使用 ViewState 或其他方法清除隐藏字段来更新 Postback 数据?
- javascript - 如何在悬停时用纯色填充渐变 svg
- marklogic - 由于文件太大,MarkLogic Corb 作业在处理过程中失败
- spring - Spring Data JPA 存储库方法查询无法解析 OrderBy 之后的属性名称
- clr-profiling-api - 我们可以在 CLR 分析中使用 IMetaDataImport2 从类中调用方法吗?
- sql-server - 存储过程的输出参数
- javascript - 是否可以在单个语句中从另一个模块导出 * 和默认值?
- json - 如何使用数据外部块将来自 Terraform null_resource 的 JSON 解析为地图
- ms-access - 如何解决“条件表达式中的数据类型不匹配”