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

China☆狼群 提交于 2019-12-11 12:09:50

问题


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');
}
});


来源:https://stackoverflow.com/questions/13302268/how-to-remove-a-href-link-tag-for-a-specific-value-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!