简体   繁体   English

使用jQuery替换li标签内的文本

[英]Replace text inside li tag using jQuery

<li ondblclick="editThisTag(this)" title="New Account0123" class="select2-selection__choice">
    <span class="select2-selection__choice__remove" role="presentation">×</span>
    Testing Text
</li>

I want to replace text inside <li> tag "Testing Text" on button click. 我想替换按钮上单击<li>标记“ Testing Text”内的文本 <li> tag have <span> tag and normal text as child. <li>标记具有<span>标记,普通文本作为子代。 <span> tag should remain as it is and only need to change "Testing Text" text on button click using jQuery. <span>标记应保持不变,只需使用jQuery单击按钮即可更改“测试文本”文本。

Get the span inside the li then get the text node after to it and update the text 获取li内的span ,然后获取其后的文本节点并更新文本

 $('#change').click(function() { $('li.select2-selection__choice') // get the span inside, and use `[0]` to get dom object .find('span')[0] // get next node after span, which is text node .nextSibling // update the text content with new value .textContent = 'text'; }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button id="change">Change Text</button> <ul> <li ondblclick="editThisTag(this)" title="New Account0123" class="select2-selection__choice"> <span class="select2-selection__choice__remove" role="presentation">×</span> Testing Text </li> </ul> 


Or following method using contents() , filter() and replaceWith() 或以下方法,使用contents() filter()replaceWith()

 $('#change').click(function() { $('li.select2-selection__choice') // get all nodes including text node and comment node .contents() // filter to get only text node and which is not empty .filter(function() { return this.nodeType === 3 && this.nodeValue.trim().length; }) // replace it with new text .replaceWith('text'); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button id="change">Change Text</button> <ul> <li ondblclick="editThisTag(this)" title="New Account0123" class="select2-selection__choice"> <span class="select2-selection__choice__remove" role="presentation">×</span> Testing Text </li> </ul> 

Having a function to find the text you want to change. 具有查找要更改的文本的功能。

jQuery("li").text(function () {
    return jQuery(this).text().replace("Testing Text", "hello world"); 
});

https://jsfiddle.net/o9va6mqe/ https://jsfiddle.net/o9va6mqe/

Working fiddle ^ 工作提琴^

Enclose with the tag the text you want to change so you can specify which part of li will be change. 用标签将要更改的文本括起来,以便您可以指定li的哪一部分将被更改。 Do it like this. 像这样做。

 $('button#changeText').click(function() { $('li.select2-selection__choice').find('font').html('HELLO WORLD'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="changeText">Change Text</button> <li title="New Account0123" class="select2-selection__choice"> <span class="select2-selection__choice__remove" role="presentation">×</span> <font>Testing Text</font> </li> 

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

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