Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何操作HTML li的文本并在主li中追加回_Javascript_Jquery_Html_Text - Fatal编程技术网

Javascript 如何操作HTML li的文本并在主li中追加回

Javascript 如何操作HTML li的文本并在主li中追加回,javascript,jquery,html,text,Javascript,Jquery,Html,Text,我试图在列表项中用一些字符串替换下划线,但在列表项的原始文本中它并没有改变。这是我的密码 $.each($.parseHTML(customFormSentenceView), function (i, item) { if ($.parseHTML(customFormSentenceView)[i].outerText.indexOf('_') > -1) { var startindex = $.parseHTML(custom

我试图在列表项中用一些字符串替换下划线,但在列表项的原始文本中它并没有改变。这是我的密码

$.each($.parseHTML(customFormSentenceView), function (i, item) {
            if ($.parseHTML(customFormSentenceView)[i].outerText.indexOf('_') > -1) {
                var startindex = $.parseHTML(customFormSentenceView)[i].outerText.indexOf("_");
                var lastindex = $.parseHTML(customFormSentenceView)[i].outerText.lastIndexOf("_");
                var len = $.parseHTML(customFormSentenceView)[i].outerText.length;
                var initstring = $.parseHTML(customFormSentenceView)[i].outerText.substring(0, startindex);
                var endstring = $.parseHTML(customFormSentenceView)[i].outerText.substring(lastindex + 1, len);
                $.parseHTML(customFormSentenceView)[i].outerText = initstring +  endstring;

            }
        });

实际上,我必须改变循环中的对象,因为这是原始对象的一部分

$.each(customFormSentenceView, function (i, item) {

            if ($(item).text().indexOf('_') > -1) {
                var startindex = $(item).text().indexOf("_");
                var lastindex = $(item).text().lastIndexOf("_");
                var len = $(item).text().length;
                var initstring = $(item).text().substring(0, startindex);
                var endstring = $(item).text().substring(lastindex + 1, len);
                refinedText += initstring  + endstring;
                $(item).text(refinedText);
            }
        });

我相信,
$.parseHTML
每次调用它时都会创建一个新对象。这意味着您正在为内存中但不在DOM中的元素设置文本,因此页面上不会有任何更改。尝试在页面上找到实际项目本身,并对其进行操作,而不是创建副本(或者创建这些副本,但完全替换DOM中的原始元素)。