【问题标题】:'addEventListener' of undefined未定义的“addEventListener”
【发布时间】:2018-12-27 03:00:02
【问题描述】:

我正在尝试使当用户点击 30 个团队中的一个时,使用 Giphy API 查询被点击的团队。

使用的 giphy API 密钥是公共 API 密钥。

// all 30 NBA Teams //
var predefinedButtons = [
  "Atlanta Hawks",
  "Boston Celtics",
  "Brooklyn Nets",
  "Charlotte Hornets",
  "Chicago Bulls",
  "Cleveland Cavaliers",
  "Dallas Mavericks",
  "Denver Nuggets",
  "Detroit Pistons",
  "Golden State Warriors",
  "Houston Rockets",
  "Indiana Pacers",
  "LA Clippers",
  "LA Lakers ",
  "Memphis Grizzlies",
  "Miami Heat",
  "Milwaukee Bucks",
  "Minnesota Timberwolves",
  "New Orleans Hornets",
  "New York Knicks",
  "Oklahoma City Thunder",
  "Orlando Magic",
  "Philadelphia Sixers",
  "Phoenix Suns",
  "Portland Trail Blazers",
  "Sacramento Kings",
  "San Antonio Spurs",
  "Toronto Raptors",
  "Utah Jazz",
  "Washington Wizards"
];
console.log(predefinedButtons);


// The Buttons added dynamically //
var $nbaTeams;

var nbaButtons = function nbaGiphy() {
  for ( i in predefinedButtons ) {
    $nbaTeams = $("<button class='.btn btn-secondary' 'onclick='getNBAGiphy()''>").text(predefinedButtons[i]);
    $("#nbaTags").append($nbaTeams);
  }

}
nbaButtons();


//  The code below is where the event listener is 'undefined'  //
function getNBAGiphy() {
  var nbaSearchGifs;
  nbaSearchGifs.addEventListener('click', function() {
    nbaSearchGifs = $(".btn btn-secondary").val();
    xhr = $.get("http://api.giphy.com/v1/gifs/search?q="+nbaSearchGifs+"&api_key=dc6zaTOxFJmzC&limit=15");
    xhr.done(function (response) {
      console.log("success got data", response);
      nbaTeamData = response.data
      $("#giphyContent").html("");
      console.log(nbaSearchGifs);
    })
  });
}
getNBAGiphy();

【问题讨论】:

  • nbaSearchGifs 是什么。$("&lt;button class='.btn btn-secondary' 'onclick='getNBAGiphy()''&gt;") 这一行也不对
  • 这只是我在函数之前声明的一个变量,看看我在哪里声明变量可能是问题所在。另外,您如何建议我修复您所指的行?

标签: javascript jquery ajax giphy-api


【解决方案1】:

您正在声明变量,但您尚未为其分配任何值。所以默认情况下它是未定义的。我指的代码是:

 function getNBAGiphy() {
    var nbaSearchGifs;
      nbaSearchGifs.addeventListner

【讨论】:

  • 你建议我如何解决这个问题?
  • 用你想添加监听器的任何内容填充它......我假设你正在尝试向按钮添加监听器并且单击它们时你已经调用了这个函数。我不知道您是否需要再次执行 addEventListener。按钮点击应该调用这个函数。否则只需阅读一些关于在按钮上设置 onClickListeners 的文档......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 2021-04-17
  • 1970-01-01
  • 2014-01-17
相关资源
最近更新 更多