简体   繁体   中英

How to Remove <a href> Link Tag for a Specific Value using Jquery

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>
$('a').filter(function(){
    return this.innerHTML === 'N';
}).replaceWith('N');

Live DEMO

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");
    }
  });
});
$('a').each(function() {
if ( $(this).text() == 'N') {
    $(this).replaceWith('N');
}
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM