如何在 jQuery 中进行“简单”查找和替换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5700818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:40:06  来源:igfitidea点击:

How to do a "simple" find and replace in jQuery?

jquerystringreplace

提问by inspite

I am new to jquery, and I'm having trouble doing what I thought should be a very simple find and replace operation.

我是 jquery 的新手,我在做我认为应该是一个非常简单的查找和替换操作时遇到了麻烦。

I want to change the text inside the the "<p>" tags. So for instance I might want to replace "your title" with "a title".

我想更改“<p>”标签内的文本。因此,例如我可能想用“标题”替换“的标题”。

  <div id="ValidationSummary">
    <ul>
        <li>
            <p>
                Please specify your title</p>
        </li>
        <li>
            <p>
                Please enter your first name</p>
        </li>
        <li>
            <p>
                Please enter your surname</p>
        </li>
    </ul>
    </div>

and here is the jQuery which isn't doing anything:

这是没有做任何事情的jQuery:

$(function() {
    $('#ValidationSummary').text().replace("your title", "a title");
    $('#ValidationSummary').text().replace("first name", "christian name");
    $('#ValidationSummary').text().replace("your surname", "your last name");
};

I really cannot see what I'm doing wrong, any ideas?

我真的看不出我做错了什么,有什么想法吗?

Please help, thanks

请帮忙,谢谢

回答by David Tang

Any changes to strings in JS always produces a new string, rather than modifying what was there. Fortunately jQuery makes it easy to both retrieve and modify text at the same time:

JS 中对字符串的任何更改总是会产生一个新的 string,而不是修改那里的内容。幸运的是,jQuery 可以轻松地同时检索和修改文本:

$('#ValidationSummary p').text(function (i, old) {
     return old
         .replace('your title', 'a title')
         .replace('first name', 'christian name')
         .replace('your surname', 'your last name');
});


Explanation:

解释:

  • returning a string from .text()tells jQuery to replace what was there (passed in as old) with this new string.

  • Since replace()returns a new string each time, we can chain multiple calls together, so that we only need one .text()call and returnstatement.

  • I used the selector '#ValidationSummary p'since using .text()or .html()will replace the entire contentsof the element it is operating on.

  • return输入一个字符串 from.text()告诉 jQueryold用这个新字符串替换那里的内容(传入 as )。

  • 由于replace()每次返回一个新字符串,我们可以将多个调用链接在一起,这样我们只需要一个.text()调用和return语句。

  • 我使用了选择器,'#ValidationSummary p'因为使用.text().html()将替换它正在操作的元素的全部内容

回答by Gaurav

$(function() {
    var text = $('#ValidationSummary').text().replace("title", "your title"); // this will give the text after replacement, but it will not set the text to ValidationSummary
    $('#ValidationSummary').text(text);

    text = $('#ValidationSummary').text().replace("first name", "christian name");
    $('#ValidationSummary').text(text);

    text = $('#ValidationSummary').text().replace("your postcode", "your zipcode");
    $('#ValidationSummary').text(text);
};

回答by Santosh Linkha

On JSFIDDLE.

JSFIDDLE 上

$(function() {
    $('#ValidationSummary').html($('#ValidationSummary').html().replace("title", "Mister"));
    $('#ValidationSummary').html($('#ValidationSummary').html().replace("first name", "ABC"));
    $('#ValidationSummary').html($('#ValidationSummary').html().replace("surname", "XYZ"));       
});

Works but don't think it's reliable.

有效,但不要认为它可靠。

回答by Vivek

TRY THIS...

尝试这个...

 $('#ValidationSummary').text($('#ValidationSummary').find('P').text().replace("title", "your title"));
 $('#ValidationSummary').text($('#ValidationSummary').find('P').text().replace("first name", "christian name"));
 $('#ValidationSummary').text($('#ValidationSummary').find('P').text().replace("your postcode", "your zipcode"));