(jQuery)未捕获类型错误:无法调用方法';更换';未定义的 $.ajax({ 错误:函数(){alert('从数据库检索候选对象时出错');}, 成功:功能(数据){ 变量模板='\r\n\ \r\n\ \r\n\ \r\n\ $5\r\n\ \r\n\ $6%\r\n\ $7,$8\r\n\ $9\r\n\ \r\n\ \r\n\ \r\n\ \r\n\ \r\n\ \r\n\ \r\n\ $10\r\n\ \r\n\ \r\n\ \r\n'; 变量结果='\r\n'; $(数据).children().first().children().each(函数()){ var-temp=模板; temp=temp.replace(/\$1/g,$(this.attr('id')); temp=temp.replace(/\$2/g,$(this).children('link').first().html()); temp=temp.replace(/\$3/g,$(this).attr('gender').toLowerCase()='m'?'him':'her'); temp=temp.replace(/\$4/g,$(this).children('name').first().html()); temp=temp.replace(/\$5/g,$(this.children('subtitle')).first().html()); 温度=温度更换(/\$6/g,”); 温度=温度更换(/\$7/g,”); 温度=温度更换(/\$8/g,”); 温度=临时维修(/\$9/g,”); temp=temp.replace(/\$10/g,$(this.children('profile').first().html()); 结果+=温度; }); 结果=$(结果); $(“#候选人”)。预编(结果); }, url:'data/candidates.xml' });

(jQuery)未捕获类型错误:无法调用方法';更换';未定义的 $.ajax({ 错误:函数(){alert('从数据库检索候选对象时出错');}, 成功:功能(数据){ 变量模板='\r\n\ \r\n\ \r\n\ \r\n\ $5\r\n\ \r\n\ $6%\r\n\ $7,$8\r\n\ $9\r\n\ \r\n\ \r\n\ \r\n\ \r\n\ \r\n\ \r\n\ \r\n\ $10\r\n\ \r\n\ \r\n\ \r\n'; 变量结果='\r\n'; $(数据).children().first().children().each(函数()){ var-temp=模板; temp=temp.replace(/\$1/g,$(this.attr('id')); temp=temp.replace(/\$2/g,$(this).children('link').first().html()); temp=temp.replace(/\$3/g,$(this).attr('gender').toLowerCase()='m'?'him':'her'); temp=temp.replace(/\$4/g,$(this).children('name').first().html()); temp=temp.replace(/\$5/g,$(this.children('subtitle')).first().html()); 温度=温度更换(/\$6/g,”); 温度=温度更换(/\$7/g,”); 温度=温度更换(/\$8/g,”); 温度=临时维修(/\$9/g,”); temp=temp.replace(/\$10/g,$(this.children('profile').first().html()); 结果+=温度; }); 结果=$(结果); $(“#候选人”)。预编(结果); }, url:'data/candidates.xml' });,jquery,google-chrome,undefined,Jquery,Google Chrome,Undefined,我一生都无法诊断这个问题。我有一个非常类似的方法从questions.xml获取问题,这个方法很好,但是这个方法在jquery.min.js文件中生成了一个错误 有什么帮助吗?可能是因为您在$9行拼写了“repalce”(而不是“replace”);-) 我能说服您使用吗?使用它实现您正在做的事情要容易得多。事实上,我将转换您的代码以使用它: $.ajax({ error: function(){alert('Error retrieving candidates from dat

我一生都无法诊断这个问题。我有一个非常类似的方法从questions.xml获取问题,这个方法很好,但是这个方法在jquery.min.js文件中生成了一个错误


有什么帮助吗?

可能是因为您在$9行拼写了“repalce”(而不是“replace”);-)

我能说服您使用吗?使用它实现您正在做的事情要容易得多。事实上,我将转换您的代码以使用它:

     $.ajax({
  error: function(){alert('Error retrieving candidates from database.');},
  success: function(data){
   var template = '    <div class="candidate" id="candidate-$1">\r\n\
     <div class="header">\r\n\
      <div class="picture"><a href="$2" class="target[\'_blank\']"><img src="pictures/$1.jpg" alt="$4" /></a></div>\r\n\
      <h2><a href="$2" class="target[\'_blank\']">$4</a></h2>\r\n\
      <h3>$5</h3>\r\n\
      <div class="stats">\r\n\
       <span class="alike" title="You have $6% in common with John Galt."><span class="number">$6</span>%</span>\r\n\
       <span class="similarity-graph" title="$7% of survey takers have less in common with $3 than you do; $8% of survey takers have more in common with $3 than you do.">$7,$8</span>\r\n\
       <span class="certainty-graph" title="PrezMatch.com is $9% certain that John Galt is your best match.">$9</span>\r\n\
      </div>\r\n\
     </div>\r\n\
     <div class="tabs">\r\n\
      <span class="active profile-tab"><a href="javascript:;">Profile</a></span>\r\n\
      <span class="keypoints-tab"><a href="javascript:;">Key points</a></span>\r\n\
     </div>\r\n\
     <div class="active content profile-content">\r\n\
      $10\r\n\
     </div>\r\n\
     <div class="content keypoints-content"></div>\r\n\
    </div>\r\n';
   var result = '\r\n';
   $(data).children().first().children().each(function(){
    var temp = template;
    temp = temp.replace(/\$1/g, $(this).attr('id'));
    temp = temp.replace(/\$2/g, $(this).children('link').first().html());
    temp = temp.replace(/\$3/g, $(this).attr('gender').toLowerCase() == 'm' ? 'him' : 'her');
    temp = temp.replace(/\$4/g, $(this).children('name').first().html());
    temp = temp.replace(/\$5/g, $(this).children('subtitle').first().html());
    temp = temp.replace(/\$6/g, '<span class="percent-in-common"></span>');
    temp = temp.replace(/\$7/g, '<span class="less-in-common"></span>');
    temp = temp.replace(/\$8/g, '<span class="more-in-common"></span>');
    temp = temp.repalce(/\$9/g, '<span class="percent-certain"></span>');
    temp = temp.replace(/\$10/g, $(this).children('profile').first().html());
    result += temp;
   });
   result = $(result);
   $('#candidates').prepend(result);
  },
  url: 'data/candidates.xml'
 });
$.ajax({
错误:函数(){
警报('从数据库检索候选人时出错');
},
成功:功能(数据){
变量模板='\r\n\
\r\n\
\r\n\
\r\n\
{id5}\r\n\
\r\n\
{id6}%\r\n\
{id7},{id8}\r\n\
{id9}\r\n\
\r\n\
\r\n\
\r\n\
\r\n\
\r\n\
\r\n\
\r\n\
{id10}\r\n\
\r\n\
\r\n\
\r\n';
变量结果='\r\n';
$(数据).children().first().children().each(函数()){
var t=$(this),temp=template.replacement({
id1:t.attr('id'),
id2:t.children('link').first().html(),
id3:t.attr('gender')。toLowerCase()='m'?'him':'her'),
id4:t.children('name').first().html(),
id5:t.children('subtitle').first().html(),
id6:“”,
id7:“”,
id8:“”,
id9:“”,
id10:t.children('profile').first().html()
});
结果+=温度;
});
$('#候选者')。前置($(结果));
},
url:'data/candidates.xml'
});
我觉得这看起来干净多了。如果您想坚持使用基于jQuery的解决方案,还可以看看

如果您使用我发布的链接中的版本,您会很高兴地知道,支持较新版本ECMAScript的浏览器将使用其内置版本覆盖它,希望能够提供不错的性能提升

作为一般性建议,我将把模板声明移到ajax调用之外,这样就不会在每次调用
success
时重新创建模板声明

$.ajax({
    error: function() {
        alert('Error retrieving candidates from database.');
    },
    success: function(data) {
        var template = '<div class="candidate" id="candidate-{id1}">\r\n\
 <div class="header">\r\n\
  <div class="picture"><a href="{id2}" class="target[\'_blank\']"><img src="pictures/{id1}.jpg" alt="{id4}" /></a></div>\r\n\
  <h2><a href="{id2}" class="target[\'_blank\']">{id4}</a></h2>\r\n\
  <h3>{id5}</h3>\r\n\
  <div class="stats">\r\n\
   <span class="alike" title="You have {id6}% in common with John Galt."><span class="number">{id6}</span>%</span>\r\n\
   <span class="similarity-graph" title="{id7}% of survey takers have less in common with {id3} than you do; {id8}% of survey takers have more in common with {id3} than you do.">{id7},{id8}</span>\r\n\
   <span class="certainty-graph" title="PrezMatch.com is {id9}% certain that John Galt is your best match.">{id9}</span>\r\n\
  </div>\r\n\
 </div>\r\n\
 <div class="tabs">\r\n\
  <span class="active profile-tab"><a href="javascript:;">Profile</a></span>\r\n\
  <span class="keypoints-tab"><a href="javascript:;">Key points</a></span>\r\n\
 </div>\r\n\
 <div class="active content profile-content">\r\n\
  {id10}\r\n\
 </div>\r\n\
 <div class="content keypoints-content"></div>\r\n\
</div>\r\n';
        var result = '\r\n';
        $(data).children().first().children().each(function(){
            var t = $(this), temp = template.supplant({
                id1: t.attr('id'),
                id2: t.children('link').first().html(),
                id3: t.attr('gender').toLowerCase() == 'm' ? 'him' : 'her'),
                id4: t.children('name').first().html(),
                id5: t.children('subtitle').first().html(),
                id6: '<span class="percent-in-common"></span>',
                id7: '<span class="less-in-common"></span>',
                id8: '<span class="more-in-common"></span>',
                id9: '<span class="percent-certain"></span>',
                id10: t.children('profile').first().html()
            });
            result += temp;
        });
        $('#candidates').prepend($(result));
    },
    url: 'data/candidates.xml'
});