How to Remove Link Tag for a Specific Value us

2019-08-02 03:55发布

问题:

I have the following situation within a report that has the following <td> element. Please note that this can appear multiples times as it is a report.

My question is, when the value is ‘N’, using jQuery, I would like to remove the whole <a href> tag and just display the value of ‘N’ alone, without the underline below it, otherwise if the value is ‘Y’ then leave it as it is.

For example:

<td align="center" headers="MPA_CHANGED"><a href="http://www.mysite.com" class="my-mpa">Y</a></td>

<td align="center" headers="MPA_CHANGED">N</td>

回答1:

$('a').filter(function(){
    return this.innerHTML === 'N';
}).replaceWith('N');

Live DEMO



回答2:

Add a class name of your choice so that you can have more control over your td elements out of all td elements present: for eg: "report_td"

so html look like this:

<td align="center" class="report_td" headers="MPA_CHANGED">...</td>

Then try like this:

$(function(){
  $("td.report_td").each(function(i, e){
    var tdElement = $(e);
    var value = tdElement.find("a").text();
    if(value == "N") {
      tdElement.html("N");
    }
  });
});


回答3:

$('a').each(function() {
if ( $(this).text() == 'N') {
    $(this).replaceWith('N');
}
});