javascript - 未定义的“addEventListener”
问题描述
我试图做到这一点,以便当用户单击 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();
解决方案
您正在声明变量,但尚未为其分配任何值。所以默认情况下它是未定义的。我指的代码是:
function getNBAGiphy() {
var nbaSearchGifs;
nbaSearchGifs.addeventListner
推荐阅读
- ios - 将焦点从一个视图控制器转移到另一个视图控制器(以模态方式呈现),反之亦然
- javascript - 以编程方式更改输入类型文件的值?
- kdb - 如何在另一个目录中安装 q?
- sapui5 - 具有 sapui5 且没有凭据的公共网站
- c# - 如何将编码的 Base64 zip 文件解码为新的 zip 文件?
- python - 使用 TensorFlow 2.0 Alpha 时无法在 Tensorboard 中看到 keras 模型图
- visual-studio-code - 如何防止 Visual Studio Code 自动删除空白行中的空格
- java - 使用 Java 检查 Git 存储库是私有的还是公共的(例如对于 GitHub)
- git - Git svn fetch 什么都不做?
- css - 如何在父div中对齐文本