使用API​​忽略AJAX模板中的单词

时间:2013-04-15 14:06:03

标签: ajax api word ignore

我正在使用api来创建一个测验。从应用程序Loopcam获取gif然后将循环标题放入div #answer中的代码如下所示:

APP = {
  refresh: function(limit, interval) {
    APP.index(limit);
    setInterval(function () {APP.index(limit)}, interval)
  },
  index: function(limit) {
    var url = 'https://api.loopc.am/v1/loops/search?q=moviecharades'

    $.ajax({
      url: url,
      dataType: "json",
      success: function(data) {
        var data = data
          , html
          , templatedata
          , template = '{{#loops}}<div id="answer">{{title}}</div><div class="loop-tumbnail"><img src="{{file_url}}" {{loop_url}}/></div>{{/loops}}'
          , offset = Math.round(Math.random() * (data.length - 1))

       TITLE = data[0].title
          console.log(offset)

        if (data.length > 0) {
          data = data.slice(offset, offset + limit);

          templatedata = {loops: data}
          html = Mustache.to_html(template, templatedata)

          $('#loops').html(html)
        }
      }
    })

我需要从div #answer中删除“#moviecharades”这个词,它始终位于从API中提取的标题中。简而言之:如果获取的标题是“#moviecharades big fish”,我希望它只能在#answer-div中说“大鱼”。

我试图把它作为#answer-div的规则,例如:

$("#answer").text($("#answer").text().replace("#moviecharades", ""));

但它不起作用......

如果有人可以提供帮助,那会非常感激!

1 个答案:

答案 0 :(得分:0)

试试这个:

var answerText = $("#answer").text();
$("#answer").text( answerText.replace("#moviecharades", ""));

希望有所帮助。