0

I need to retrive the id of a <td> having only the text content of the <td>.

For example:

.....
<tr>
    <td id='hey'>Hello world!</td>
</tr>
....

If I know the text "Hello world", is it possible to retrive the id of the <td>?

/*****/ Thanks to all guys! I will try the solutions offered as soon as possible!! Just to explain i was working with OBIEE 11g and i was trying to write a script to add format condition to grand total that is not supported. Till now i came up with this solution:

    <script type="text/javascript">
var threshold = 10; // set the threshold
/*** Definizione colonne da formattare ***/
/*** Partire da 0 ***/
var colonneFormattate = new Array(2);
colonneFormattate[0] = 0;
colonneFormattate[1] = 2;
/************************************************/
var tds = document.getElementsByClassName('PTDT');
for(var td =0; td < colonneFormattate.length;td++){
    var amount = (tds[colonneFormattate[td]].innerHTML.replace("€","")).replace(/&nbsp;/g,'');
    //alert(colonneFormattate[td]);
    var lungh = amount.indexOf(",");
            var novirgole = amount.substring(0,lungh);
            var novirgole = novirgole.replace(/\./g,"");
    var num = parseInt(novirgole);
    if(num > threshold){
        tds[colonneFormattate[td]].style.color = "green";
    }else{
        tds[colonneFormattate[td]].style.color = "red";
    }
}
</script>

So in this case the grand total will be coloured if the number is higher then threshold. I ask your help to try to define the column to search in an array to make it more simple for the user. I mean if the user want to format the column named Turnover, it will add in the script the column that he needs.

I will try your suggestion! And i will let you know!

Cheers!

4

6 回答 6

5

这是一个解决方案,它只匹配一个精确匹配,而不仅仅是包含文本,这个搜索不会在第一次匹配后停止,而是会一直持续到没有更多元素可以搜索

HTML

<table>
    <thead></thead>
    <tbody>
        <tr>
            <td id='hey'>Hello world!</td>
            <td id='hey2'>Hello world 2!</td>
            <td id='hey3'>Hello world 3!</td>
        </tr>
    </tbody>
</table>

Javascript

var search = "Hello world!";

Array.prototype.forEach.call(document.getElementsByTagName("td"), function (td) {
    if (td.textContent === search) {
        alert(td.id);
    }
});

jsfiddle 上

如果您想要一个解决方案,在文本中搜索并在第一次匹配后停止,那么您可以这样做

var search = "Hello world!";

Array.prototype.some.call(document.getElementsByTagName("td"), function (td) {
    if (td.textContent.indexOf(search) !== -1) {
        alert(td.id);
        return true;
    }

    return false;
});

jsfiddle 上

于 2013-05-02T16:12:02.570 回答
2

尝试这个:

演示

var searchString = "Hello world!";
var tds = document.getElementsByTagName("td");

[].forEach.call(tds, function (td) {
    if (td.textContent.indexOf(searchString) > -1) { // For exact match use td.textContent === searchString 
        alert(td.id);
    }
});

查看兼容性:

于 2013-05-02T16:06:39.280 回答
2

对,我的“没有 JQuery”很粗糙,但这里是:

var matchedId;

var elements = document.getElementsByTagName("td");

for(var i = 0; i < elements.length; i++){
   var element = elements[i];
   if(element.innerHTML.indexOf("Hello world!") != -1){
      matchedId = element.id;
      break;
   }
}

这是一个工作示例

于 2013-05-02T16:10:35.530 回答
2

jQuery 提供了:contains选择器:

$('td:contains("Hello world!")')[0].id

但是,如果没有它,您需要手动完成:

var tds = document.getElementsByTagName('td');
var id;

outer: for(var i = 0; i < tds.length; i++) {
    var td = tds[i];

    for(var j = 0; j < td.childNodes.length; j++) {
        var child = td.childNodes[j];

        if(child.nodeType === 3 && child.nodeValue.indexOf('Hello world!') !== -1) {
            id = td.id;
            break outer;
        }
    }
}

请注意,如果您不希望与 具有相同级别的兼容性:contains,请td.textContent.indexOf('Hello, world!')改为检查。

于 2013-05-02T16:11:09.210 回答
1

您可以执行以下操作:

var tdarr = document.getElementsByTagName("td");

for (var i = 0; i < tdarr.length; i++) {
    if (tdarr[i].innerHTML === "Hello world!") {
        alert(tdarr[i].id);
    }
}

JSFiddle

于 2013-05-02T16:09:52.050 回答
0

好了朋友们!感谢大家,我使用了您的代码,并以这种方式实现了我的目标:

for(var td =0; td < tds.length;td++){
    //alert(tds[td].textContent);
    if(tds[td].textContent == colonna[0]){
        var id = tds[td].id;
        var numcol = id.substring(id.lastIndexOf("_")+1,id.length);
        colonna[4] = parseInt(numcol);
        td = tds.length;
    }
}

谢谢你的建议!!

干杯!

于 2013-05-03T12:35:29.733 回答