Spliter如何将<span>的文本作为输出,

2024-09-30 05:27:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个<tr>,它的id是由Javascript代码动态生成的,所以我不能使用find_by_id来选择它。如果我不知道包含<tr>标记的id是什么,如何使用spliter访问span中的文本?你知道吗

<tr id="treeview-1020-record-b3250a58-46d6-4df8-9e04-69d82afcc966" data-boundview="treeview-1020" data-recordid="b3250a58-46d6-4df8-9e04-69d82afcc966" data-recordindex="22" class="x-grid-row x-grid-tree-node-expanded x-grid-data-row" tabindex="-1">
    <td class="x-grid-cell x-grid-td x-grid-cell-treecolumn-1019 x-grid-cell-treecolumn x-grid-cell-first x-grid-cell-last x-unselectable x-grid-cell-treecolumn">
        <div unselectable="on" class="x-grid-cell-inner" style="text-align:left;">
            <span class="x-tree-node-text">WZ1MZ35_G2 ADV.</span>
        </div>
    </td>
</tr>

Tags: idnodetreedatacelltrclassgrid
1条回答
网友
1楼 · 发布于 2024-09-30 05:27:36

选择所有tr标记。 找到以正确字符串开头的。 从该节点搜索所有跨度标记。并提取文本

allTR = document.getElementsByTagName('tr');
for (let trIdx = 0; trIdx < allTR.length; trIdx++) {
    const trElem = allTR[trIdx];
    if (trElem.getAttribute('id').startsWith('treeview-1020-')) {
        allSpan = trElem.getElementsByTagName('span');
        if (allSpan.length === 0) continue;
        spanText = allSpan[0].textContent
        // do something usefull with the text
    }
}

相关问题 更多 >

    热门问题