使用jQuery替换Li文本 [英] Replace li text using jquery

查看:220
本文介绍了使用jQuery替换Li文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试替换li和标签的内容.

I am trying to replace the content of li and tags.

尝试以下代码.

   <html>
    <head>Sample</head>
   <body>
    <a href="#"><input type="checkbox" class="layer-input-filter" name="testname">Test <span class="count">5</span>      </a>

    <a href="#"><input type="checkbox" class="layer-input-filter" name="testname1">Test1 <span class="count">10</span> </a>

 <ul>
  <li><a href="#" rel="noopener">Mixed assessment guidelines</a></li>
  <li><a href="" target="_blank" rel="noopener">Specimen </a></li>
  <li><a href="#" rel="noopener">Plagiarism </a></li>
  <li><a href="" target="_blank" rel="noopener">Download </a></li>
  <li><a href="#" rel="noopener">Qualification update</a></li>
  <li><a href="" target="_blank" rel="noopener">Specimen</a></li>
  <li><a href="#" rel="noopener">Exam guide update</a></li>
  <li><a href="" target="_blank" rel="noopener">Guide </a></li>
 </ul>

  <script>
     $(document).ready(function($) {
      var value = 0.4;
      $("body").find('li').each(function (index) {
                if ($(this).text() != '') {

                    var str = $(this).clone().children().remove().end().text();
                    var newstring =  str + str.substring(0, str.length  * value);
                    $(this).text(newstring);
                }
            });
    });
   </script>
 </body>

  </html>

我需要替换体内每个li的文本.因此在jquery上面使用它.但它不起作用.

I need to replace the text of every li inside the body. so used above jquery for it. But its not working.

示例:

  <li><a href="#" rel="noopener">Plagiarism </a></li>

应替换为

   <li><a href="#" rel="noopener">PlagiarismPlag </a></li>

每个li文本的前四个字符.在上面的示例中为标记

First four characters of each li text. That is Plag in above example

有人可以帮我吗?

推荐答案

我认为问题出在线

 var str = $(this).clone().children().remove().end().text();

函数的顺序使字符串最后为空.如果将其替换为

The order of the functions makes the string empty in the end. If you replace it with

 var str = $(this).text();

有效.

我不确定您的生产线要实现什么目标,但是问题就在那里.

I'm not sure what your line is trying to achieve, but the problem is there.

这篇关于使用jQuery替换Li文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆