JQuery删除超链接并替换为文本

JQuery remove hyperlink and replace with text

本文关键字:文本 替换 删除 超链接 JQuery      更新时间:2023-09-26

如何从li中删除超链接并用其他文本替换它?

<li class="pull-left">
     <a href="#" class="js-close-post" data-post-id="1">
       Close
     </a>
 </li>

下面删除了整个li。我想要的是淡出链接并使用文本Closed 淡入

<li class="pull-left">
    Closed
 </li>
  $(".js-close-post").click(function (e) {
        var link = $(e.target);
        link.parents("li").fadeOut(function () {
             $(this).remove();
        });
  });

<li>上使用text('Closed')html('Closed')将删除<a>

尝试

$(".js-close-post").click(function (e) {  
     // "this" is the <a>      
     var $li = $(this).closest("li").fadeOut(function () {              
          $li.text('Closed').fadeIn();
     });
});

您可以为此使用jQuery.replaceWith()。

  $(".js-close-post").click(function (e) {
        var link = $(e.target);
        $a = $(this);
        $a.parents("li").fadeOut(function () {
             $a.replaceWith($a.text());
        }).fadeIn();
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="pull-left">
     <a href="#" class="js-close-post" data-post-id="1">
       Close
     </a>
 </li>

尝试以下操作:

$(".js-close-post").click(function () {
    $(this).fadeOut("slow", function () {
        var parent = $(this).parent();
        var closedElement = parent.append("Closed").hide();    
        closedElement.fadeIn("slow");
        $(this).remove();
   });
});

https://jsfiddle.net/noLscfh9/