【问题标题】:How to do a "simple" find and replace in jQuery?如何在 jQuery 中进行“简单”查找和替换?
【发布时间】:2011-08-07 17:36:20
【问题描述】:

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

我想更改“

”标签内的文本。因此,例如,我可能想将“your title”替换为“a title”。

  <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>

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

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

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

请帮忙,谢谢

【问题讨论】:

  • .text() 不应该这样使用,它应该是: $('#ValidationSummary').text($(this).text().replace("your title ", "一个标题"));

标签: jquery string replace


【解决方案1】:

在 JS 中对字符串的任何更改总是会产生一个新字符串,而不是修改那里的内容。幸运的是,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');
});

解释:

  • returning 来自.text() 的字符串告诉 jQuery 用这个新字符串替换那里的内容(作为 old 传入)。

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

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

【讨论】:

    【解决方案2】:
    $(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);
    };
    

    【讨论】:

    • 非常感谢,这几乎是完美的,但接受的答案是完美的。
    【解决方案3】:

    开启JSFIDDLE

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

    有效,但认为它不可靠。

    【讨论】:

      【解决方案4】:

      试试这个...

       $('#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"));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-08
        • 2011-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-24
        • 1970-01-01
        • 2011-06-07
        相关资源
        最近更新 更多