简体   繁体   English

如何替换“li”中的文本

[英]How to replace text in a “li”

I have about 100 pages all with an <ul> - the problem is, I need to wrap each list item in a <span> . 我有大约100页的所有<ul> - 问题是,我需要将每个列表项包装在<span> I've added the code below 我已添加以下代码

<ul style="list-style-type: disc; margin-left: 40px; line-height: 140%;" _mce_style="list-style-type: disc; margin-left: 40px; line-height: 140%;">
    <li> ICD-10 transition </li>
    <li> Pricing transparency </li>
    <li>Patient and payor mix fluctuation<br>&nbsp;</li>
</ul>  

$(document).ready(function () {
    $("li").each(function (index) {
        $("li:nth-last-child(" + index + ")").append(" <span>" + $("li:nth-last-child(" + index + ")").text() + "</span>");
    });
});

This is the result I get: 这是我得到的结果:

ICD-10 transition ICD-10 transition ICD-10 transition ICD-10过渡ICD-10过渡ICD-10过渡

Pricing transparency Pricing transparency Pricing transparency 定价透明度定价透明度定价透明

Patient and payor mix fluctuation 患者和付款人混合波动

Patient and payor mix fluctuation Patient and payor mix fluctuation 患者和付款人混合波动患者和付款人混合波动

The code is being replaced twice when I only want the code to run once and replace the code one time - not twice. 当我只希望代码运行一次并替换代码一次而不是两次时,代码被替换两次。 I'd like to know how to fix this but more so why this is even happening in the first place. 我想知道如何解决这个问题,但更多的是为什么这首先发生在一起。 The method isn't being called twice - so why is it happening. 该方法没有被调用两次 - 所以它为什么会发生。


Thank you guys so much for helping me figure out!!! 非常感谢你们帮助我弄清楚!!! I got it working with this... 我得到了它...

  $(document).ready(function () {

        $("li").each(function (index) {
            $(this).html("<span>" + $(this).html() + "</span>");

        });
    });

Thank you so very much!!!!!!!! 非常感谢你!!!!!!!!

You can do it like following. 你可以像下面这样做。

$("li").each(function () {
    $(this).html('<span>'+$(this).html()+'</span>');
});

This is happening because when you append the span you keep the text in the li , so it is showed twice. 发生这种情况是因为当您附加span时,您将文本保留在li ,因此会显示两次。 You can change the append() to html() in order to clear the content in the li and replace with the new span : 您可以将append()更改为html()清除 li的内容并替换为新的span

$("li:nth-last-child(" + index + ")").html(" <span>" + $("li:nth-last-child(" + index + ")").text() + "</span>");

Working demo 工作演示

$("li:nth-last-child(" + index + ")").append(" <span>" + $("li:nth-last-child(" + index + ")").text() + "</span>");

Append adds onto the list element content. 追加添加到列表元素内容。

You can do something like: 你可以这样做:

$(document).ready(function () {
    $("li").each(function (index, el) {
        $(el).html(" <span>" + $(el).text() + "</span>");
    });
});
$(document).ready(function() {
   $("li").wrap("<span></span>");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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