- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一些代码来测试我第一次使用外部 API 的技能。
我已经设法让页面填充 API 信息,但我需要这样做,以便用户可以通过在搜索栏中键入来更改 API 信息的读出。我使用 Bootstrap 3 编码在导航栏元素中放置了一个搜索栏。这是导航栏的代码:
<nav class="navbar navbar-default navbar-expand-xl" id="filterBar"><a class="navbar-brand navbar-left" id="filterBarBrand">The Set List</a>
<div class="container" id="navContain2" style="margin-right: 80px;">
<form class="navbar-form navbar-right" style="padding-left: 50px;">
<div class="form-group">
<input id="enterText" type="text" class="form-control" onKeyUp="getJSON()" onChange="getJSON()" placeholder="Search for Beer Name or Type" style="width: 300px;">
</div>
<button type="button" class="btn btn-default" id="searchSubmit" onClick="getJSON()"><i class="glyphicon glyphicon-search"></i></button>
</form>
<div class="collapse navbar-collapse" id="myFilter">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><strong>FILTER BY</strong></a>
<ul class="dropdown-menu" id="fliter">
<li class="dropdown-header"><strong>FLAVOR NOTES</strong></li>
<label class="checkbox-inline"><input type="checkbox" value="">Hoppy</label>
<label class="checkbox-inline"><input type="checkbox" value="">Citrus</label>
<label class="checkbox-inline"><input type="checkbox" value="">Malty</label>
<label class="checkbox-inline"><input type="checkbox" value="">Herbal</label>
<label class="checkbox-inline"><input type="checkbox" value="">Bitter</label>
<label class="checkbox-inline"><input type="checkbox" value="">Fruity</label>
<label class="checkbox-inline"><input type="checkbox" value="">Toasty</label>
<label class="checkbox-inline"><input type="checkbox" value="">Earthy</label>
<label class="checkbox-inline"><input type="checkbox" value="">Sour</label>
<label class="checkbox-inline"><input type="checkbox" value="">Dry</label>
<label class="checkbox-inline"><input type="checkbox" value="">Caramel</label>
<label class="checkbox-inline"><input type="checkbox" value="">Smokey</label>
<label class="checkbox-inline"><input type="checkbox" value="">Tart</label>
<label class="checkbox-inline"><input type="checkbox" value="">Crisp</label>
<label class="checkbox-inline"><input type="checkbox" value="">Toffee</label>
<label class="checkbox-inline"><input type="checkbox" value="">Spicey</label>
<li class="dropdown-header"><strong>IBU</strong></li>
<li class="dropdown-header"><strong>ABV</strong></li>
</ul>
</li>
</ul>
</div>
</div>
</nav> <!-- End Navbar -->
只要用户在搜索栏中键入内容(id =“#enterText”),它就会调用函数“getJSON”。
我有一个外部 .js 文件 (JSON.js),其中包含我的 JavaScript 代码:
var api = 'https://api.punkapi.com/v2/beers?beer_name=';
var input = "punk";
var url = api + input;
//document.getElementById('enterText').value;
$.getJSON(url, function(data){
"use strict";
console.log(input);
$.each(data, function(index, value){
console.log(value);
var imageURL = value.image_url;
var name = value.name;
var tag = value.tagline;
var ibu = value.ibu;
var abv = value.abv;
var desc = value.description;
var food1 = value.food_pairing[0];
var food2 = value.food_pairing[1];
var food3 = value.food_pairing[2];
// $('.image img').attr('src', imageURL);
// $('.name').text(name);
// $('.tag').text(tag);
// $('.ibu').text(ibu);
// $('.abv').text(abv);
// $('.desc').text(desc);
// $('.food1').text(food1);
// $('.food2').text(food2);
// $('.food3').text(food3);
$('.output').append('<div class="row" id="beerRow"><div class="col-sm-3"><div class="image" align="right"><img src="' + imageURL + '"/ height="350px"></div><br><br></div><div class="col-sm-6"><h2 class="name">' + name + '</h2><h3 class="tag">' + tag + '</h3><span id="numbers"><p>IBU   </p> <p class="ibu" style="color: #929292; font-size: 1.15em">' + ibu + '</p><p>  ABV   </p> <p class="abv" style="color: #929292; font-size: 1.15em">' + abv + '</p></span><p class="desc">' + desc + '</p><div class="row"><div class="col-sm-1" align="right"><img src="Images/icon_food_pairing.svg" height="30" width="30"></div><div class="col-sm-11" align="left"><h4 class="food1">' + food1 + '</h4><h4 class="food2">' + food2 + '</h4><h4 class="food3">' + food3 + '</h4></div></div></div></div><br><br><br>');
});
});
我已经将 .js 文件加载到 HTML 文件的头部:
<head>
<title>Punk Out Brews</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet" type="text/css">
<link rel="icon" type="image/png" sizes="32x32" href="Images/icon_beer_mug.svg"/>
<link rel="icon" type="image/png" sizes="16x16" href="Images/icon_beer_mug.svg"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="CSS.css">
<script src="JSON.js"></script>
</head>
出于某种原因,控制台出现以下错误:
“类型错误:document.getElementById(...) 为空”
当我尝试在我创建的搜索栏中输入内容时,出现以下错误:
“ReferenceError:未定义 getJSON”
我已尽一切努力确保这两个文件正在通信。我什至尝试将它们放在同一个文件中,但得到了相同的结果。知道为什么这不起作用吗?
最佳答案
尝试将您的调用放在一个名为 getJSON() 的函数中,例如:
function getJSON(){
$.getJSON(url, function(data){
"use strict";
console.log(input);
$.each(data, function(index, value){
console.log(value);
var imageURL = value.image_url;
var name = value.name;
var tag = value.tagline;
var ibu = value.ibu;
var abv = value.abv;
var desc = value.description;
var food1 = value.food_pairing[0];
var food2 = value.food_pairing[1];
var food3 = value.food_pairing[2];
// $('.image img').attr('src', imageURL);
// $('.name').text(name);
// $('.tag').text(tag);
// $('.ibu').text(ibu);
// $('.abv').text(abv);
// $('.desc').text(desc);
// $('.food1').text(food1);
// $('.food2').text(food2);
// $('.food3').text(food3);
$('.output').append('<div class="row" id="beerRow"><div class="col-sm-3"><div class="image" align="right"><img src="' + imageURL + '"/ height="350px"></div><br><br></div><div class="col-sm-6"><h2 class="name">' + name + '</h2><h3 class="tag">' + tag + '</h3><span id="numbers"><p>IBU   </p> <p class="ibu" style="color: #929292; font-size: 1.15em">' + ibu + '</p><p>  ABV   </p> <p class="abv" style="color: #929292; font-size: 1.15em">' + abv + '</p></span><p class="desc">' + desc + '</p><div class="row"><div class="col-sm-1" align="right"><img src="Images/icon_food_pairing.svg" height="30" width="30"></div><div class="col-sm-11" align="left"><h4 class="food1">' + food1 + '</h4><h4 class="food2">' + food2 + '</h4><h4 class="food3">' + food3 + '</h4></div></div></div></div><br><br><br>');
});
});}
关于javascript - 在外部 JavaScript 文件中调用函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51604100/
为了让我的代码几乎完全用 Jquery 编写,我想用 Jquery 重写 AJAX 调用。 这是从网页到 Tomcat servlet 的调用。 我目前情况的类似代码: var http = new
我想使用 JNI 从 Java 调用 C 函数。在 C 函数中,我想创建一个 JVM 并调用一些 Java 对象。当我尝试创建 JVM 时,JNI_CreateJavaVM 返回 -1。 所以,我想知
环顾四周,我发现从 HTML 调用 Javascript 函数的最佳方法是将函数本身放在 HTML 中,而不是外部 Javascript 文件。所以我一直在网上四处寻找,找到了一些简短的教程,我可以根
我有这个组件: import {Component} from 'angular2/core'; import {UserServices} from '../services/UserService
我正在尝试用 C 实现一个简单的 OpenSSL 客户端/服务器模型,并且对 BIO_* 调用的使用感到好奇,与原始 SSL_* 调用相比,它允许一些不错的功能。 我对此比较陌生,所以我可能会完全错误
我正在处理有关异步调用的难题: 一个 JQuery 函数在用户点击时执行,然后调用一个 php 文件来检查用户输入是否与数据库中已有的信息重叠。如果是这样,则应提示用户确认是否要继续或取消,如果他单击
我有以下类(class)。 public Task { public static Task getInstance(String taskName) { return new
嘿,我正在构建一个小游戏,我正在通过制作一个数字 vector 来创建关卡,该数字 vector 通过枚举与 1-4 种颜色相关联。问题是循环(在 Simon::loadChallenge 中)我将颜
我有一个java spring boot api(数据接收器),客户端调用它来保存一些数据。一旦我完成了数据的持久化,我想进行另一个 api 调用(应该处理持久化的数据 - 数据聚合器),它应该自行异
首先,这涉及桌面应用程序而不是 ASP .Net 应用程序。 我已经为我的项目添加了一个 Web 引用,并构建了各种数据对象,例如 PayerInfo、Address 和 CreditCard。但问题
我如何告诉 FAKE 编译 .fs文件使用 fsc ? 解释如何传递参数的奖励积分,如 -a和 -target:dll . 编辑:我应该澄清一下,我正在尝试在没有 MSBuild/xbuild/.sl
我使用下划线模板配置了一个简单的主干模型和 View 。两个单独的 API 使用完全相同的配置。 API 1 按预期工作。 要重现该问题,请注释掉 API 1 的 URL,并取消注释 API 2 的
我不确定什么是更好的做法或更现实的做法。我希望从头开始创建目录系统,但不确定最佳方法是什么。 我想我在需要显示信息时使用对象,例如 info.php?id=100。有这样的代码用于显示 Game.cl
from datetime import timedelta class A: def __abs__(self): return -self class B1(A):
我在操作此生命游戏示例代码中的数组时遇到问题。 情况: “生命游戏”是约翰·康威发明的一种细胞自动化技术。它由一个细胞网格组成,这些细胞可以根据数学规则生存/死亡/繁殖。该网格中的活细胞和死细胞通过
如果我像这样调用 read() 来读取文件: unsigned char buf[512]; memset(buf, 0, sizeof(unsigned char) * 512); int fd;
我用 C 编写了一个简单的服务器,并希望调用它的功能与调用其他 C 守护程序的功能相同(例如使用 ./ftpd start 调用它并使用 ./ftpd stop 关闭该实例)。显然我遇到的问题是我不知
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
在 dos 中,当我粘贴此命令时它会起作用: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.
我希望能够从 cmd 在我的 Windows 10 计算机上调用 python3。 我已重新安装 Python3.7 以确保选择“添加到路径”选项,但仍无法调用 python3 并使 CMD 启动 P
我是一名优秀的程序员,十分优秀!