简体   繁体   English

Javascript替换不适用于jQuery

[英]Javascript replace not working with jQuery

So I am using jQuery to load in an html file then do a simple string replace on some tokens. 因此,我正在使用jQuery加载html文件,然后对某些标记进行简单的字符串替换。 However they will not do the replace. 但是,他们将不会进行替换。 Can someone explain to me why this is not working with the replace calls? 有人可以向我解释为什么替换调用不起作用吗?

pendingRow.html endingRow.html

<span id="{pendingDbId}" data-database="{pendingName}">
    {pendingName} ({pendingTime}) - <a id="cancel-{pendingDbId}" data-dbId="{pendingDbId}" href="#">Cancel</a>
    <br />
</span>

jQuery jQuery的

$('#pendingLoadDiv').load('templates/pendingRow.html', function() {
                            $('#pendingLoadDiv').html($('#pendingLoadDiv').html().replace("{pendingName}", $('#database option:selected').text()));
                            $('#pendingLoadDiv').html($('#pendingLoadDiv').html().replace("{pendingDbId}", $('#database').val()));
                            $('#pendingLoadDiv').html($('#pendingLoadDiv').html().replace("{pendingTime}", "--:--:--"));
                            $('#pendingDiv').append($('#pendingLoadDiv').html());
                        });

It is getting all the way to the append at the bottom with no errors however it is not replacing any text. 它一直到底部的追加都没有错误,但是没有替换任何文本。

I have even tried storing the pendingLoadDiv to a variable then running the replace calls on the variable and still am having the same issue. 我什至尝试将未完成的LoadDiv存储到变量中,然后在该变量上运行替换调用,仍然遇到相同的问题。

Any ideas? 有任何想法吗?

You really should not use load, I am sure the browser freaks out with the invalid characters in the id. 您确实不应该使用负载,我敢肯定浏览器会在id中包含无效字符。 Plus you are doing all of this DOM look ups when you can just work with the string directly. 另外,当您可以直接使用字符串时,就可以进行所有此类DOM查找。

$.ajax({
  url: "templates/pendingRow.html",
  success: function( data ){
        var html = data.replace("{pendingName}", $('#database option:selected').text())
                        .replace("{pendingDbId}", $('#database').val())
                        .replace("{pendingTime}", "--:--:--");
        $('#pendingDiv').append( html );
  }
});

In the load success handler it has not yet updated the container. load成功处理程序中,它尚未更新容器。 You should first set the html into the container and then try to replace. 您应该首先将html设置到容器中,然后尝试替换。

$('#pendingLoadDiv').load('templates/pendingRow.html', function(data) {
  var $pendingLoadDiv = $('#pendingLoadDiv').html(data);
  var markup = $pendingLoadDiv.html(); 

  markup = markup.replace("{pendingName}", $('#database option:selected').text());
  markup = markup.replace("{pendingDbId}", $('#database').val());
  markup = markup.replace("{pendingTime}", "--:--:--");

   $('#pendingDiv').append(markup);
});

.replace() will only replace the first instance of the string, then return. .replace()将仅替换字符串的第一个实例,然后返回。 To do a replaceAll, use a regular expression with the global flag: http://jsfiddle.net/RwPuu/ 要执行replaceAll,请使用带有全局标志的正则表达式: http : //jsfiddle.net/RwPuu/

$('#pendingLoadDiv').html($('#pendingLoadDiv').html().replace(/{pendingName}/g, "replacingString"));

EDIT: The jQuery API documentation also seems to state, contrary to the other answers, that the selected element's content is replaced, then the callback is run. 编辑:jQuery API文档似乎还指出,与其他答案相反,替换了所选元素的内容, 然后运行了回调。 http://api.jquery.com/load http://api.jquery.com/load

I'd guess that at the time the inner function has been called, Jquery has not yet updated the contents of your div with the loaded data, so you're search/replacing the OLD data, which is about to get deleted. 我猜想在调用内部函数时,Jquery尚未使用加载的数据更新div的内容,因此您正在搜索/替换即将删除的OLD数据。

Try 尝试

$('#pendingLoadDiv).load('templates/pendingRow.html', function(data) {
                                                               ^^^^^--- the loaded data
   data.replace("{pendingName}", $('#database option:selected').text()));
   etc...
   $('#pendingDiv').append(data);
}

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

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