检查单元格中的文本是否为粗体 [英] Check if text in cell is bold

查看:117
本文介绍了检查单元格中的文本是否为粗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在html中有一个表格。有些单元格大胆,有些是正常的。我如何检查某个单元格是否为粗体(例如i,j),并获取该单元格的文本而没有标签。

单元格只能有文本。如果它是粗体的 - 它应该包含标签。例如:

 < tr> 
< td>
不是粗体文本
< / td>
< td>
< b>粗体文字< / b>
< / td>
< / tr>

PS我无法使用类或id属性。
PSS没有jQuery代码会更好,我们现在还没有使用它

解决方案

看到这个答案如何获取对单元格的引用: https://stackoverflow.com/a/3052862/34088



您可以使用 cell.firstChild 获取第一个孩子。这给你一个文本节点或一个< B> 节点。您可以使用 node.nodeType 来检查它,DOM节点为1,文本节点为3。其中给出:

 函数isBold(table,rowIdx,columnIdx){
var row = table.rows [rowIdx] ;
var cell = row.cells [columnIdx];
var node = cell.firstChild;
if(node.nodeType === 3){
return false;
}

返回node.nodeName ==='b'|| node.nodeName ==='B';
}


I have a table in html. Some cells is bold and some is normal. How I can check if some cell is bold (for example i,j) and get text of that cell without tag .

Cells can have only text. If it bold - it should contain tag . For example:

<tr>
<td>
Not bold text
</td>
<td>
<b> Bold text </b>
</td>
</tr>

PS I can't use class or id properties. PSS It would be better without jquery code, we are not using it for now

解决方案

See this answer how to get a reference to the cell: https://stackoverflow.com/a/3052862/34088

You can get the first child with cell.firstChild. This gives you a text node or a <B> node. You can check this with the node.nodeType which is 1 for DOM nodes or 3 for text nodes. Which gives:

function isBold(table, rowIdx, columnIdx) {
    var row = table.rows[rowIdx];
    var cell = row.cells[columnIdx];
    var node = cell.firstChild;
    if( node.nodeType === 3 ) {
        return false;
    }

    return node.nodeName === 'b' || node.nodeName === 'B';
}

这篇关于检查单元格中的文本是否为粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆