如何替换&#39;&lt;p&gt;&#39;与&#39;<li&gt;&#39;在 jQuery 的数组中?

如何替换&#39;&lt;p&gt;&#39;与&#39;<li&gt;&#39;在 jQuery 的数组中?

问题描述:

假设我有这个

$(document).ready(function() {
   var array = $.makeArray($('p'));
   $(array).appendTo(document.body);
   });
});

<p>how</p>
<p>are</p>
<p>you</p>
<p>baby?</p>

如果我想用

  • 替换

    并且预期输出是 ...

    If I want to replace <p> with <li> and expected output is ...

    <li>how</li>
    <li>are</li>
    <li>you</li>
    <li>baby?</li>
    

    我该怎么办?提前致谢!

    What should I do? Thanks in advance!

  • $("p").each(function () {
         $(this).replaceWith("<li>" + $(this).html() + "</li>");
    });