Open NBR-hugh opened 8 years ago
Trigger Click Events with jQuery total:10min
Change Text with Click Events TOTAL:10MIN
Get JSON with the jQuery getJSON Method TOTAL:35MIN
Convert JSON Data to HTML TOTAL:50MIN
Render Images from Data Sources TOTAL:13MIN
Prefilter JSON TOTAL:5MIN
Get Geo-location Data TOTAL:15MIN
在 $(document).ready(function() {}添加 $("#getMessage").on("click",function(){});实现点击事件控制(a click event handler)
$(document).ready(function() {
// Only change code below this line.
$("#getMessage").on("click",function(){});
// Only change code above this line.
});
<script>
$(document).ready(function() {
// Only change code below this line.
$("#getMessage").on("click",function(){});
// Only change code above this line.
});
</script>
<div class="container-fluid">
<div class = "row text-center">
<h2>Cat Photo Finder</h2>
</div>
<div class = "row text-center">
<div class = "col-xs-12 well message">
The message will go here
</div>
</div>
<div class = "row text-center">
<div class = "col-xs-12">
<button id = "getMessage" class = "btn btn-primary">
Get Message
</button>
</div>
</div>
</div>
$(".message").html("Here is the message");
实现功能:当用户点击按钮时,文本从The message will go here变成Here is the message,注意命令中message是所修改的html元素的class:<div class = "col-xs-12 well message">
The message will go here
</div>
<script>
$(document).ready(function() {
$("#getMessage").on("click", function(){
// Only change code below this line.
$(".message").html("Here is the message");
// Only change code above this line.
});
});
</script>
<div class="container-fluid">
<div class = "row text-center">
<h2>Cat Photo Finder</h2>
</div>
<div class = "row text-center">
<div class = "col-xs-12 well message">
The message will go here
</div>
</div>
<div class = "row text-center">
<div class = "col-xs-12">
<button id = "getMessage" class = "btn btn-primary">
Get Message
</button>
</div>
</div>
</div>
按要求手动添加
$.getJSON("/json/cats.json",function(json){
$(".message").html(JOSN.stringify(json));
});
结果无法通过测试,于是直接复制黏贴命令看是否是输入错误,通过挑战,确定为输入错误。 原命令为:
$.getJSON("/json/cats.json", function(json) {
$(".message").html(JSON.stringify(json));
});
发现错误为将JSON 输成 JOSN,戒之戒之!!!
<script>
$(document).ready(function() {
$("#getMessage").on("click", function(){
// Only change code below this line.
$.getJSON("/json/cats.json",function(json){
$(".message").html(JSON.stringify(json));
});
// Only change code above this line.
});
});
</script>
<div class="container-fluid">
<div class = "row text-center">
<h2>Cat Photo Finder</h2>
</div>
<div class = "row text-center">
<div class = "col-xs-12 well message" >
The message will go here
</div>
</div>
<div class = "row text-center">
<div class = "col-xs-12">
<button id = "getMessage" class = "btn btn-primary">
Get Message
</button>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$("#getMessage").on("click", function() {
$.getJSON("/json/cats.json", function(json) {
var html = "";
// Only change code below this line.
json.forEach(function(val){
var keys =Object.keys(val);
html +="<div class='cat'>";
keys.forEach(function(key){
html += "<strong>"+ key +"</strong>"+ val[key]+"<br>";
});
html +="</div><br>";
});
// Only change code above this line.
$(".message").html(html);
});
});
});
</script>
<div class="container-fluid">
<div class = "row text-center">
<h2>Cat Photo Finder</h2>
</div>
<div class = "row text-center">
<div class = "col-xs-12 well message">
The message will go here
</div>
</div>
<div class = "row text-center">
<div class = "col-xs-12">
<button id = "getMessage" class = "btn btn-primary">
Get Message
</button>
</div>
</div>
</div>
html += "<img src = '"+val.imageLink + "'" +"alt='" + val.altText + "'>";
便可以提取图片<script>
$(document).ready(function() {
$("#getMessage").on("click", function() {
$.getJSON("/json/cats.json", function(json) {
var html = "";
json.forEach(function(val) {
html += "<div class = 'cat'>";
// Only change code below this line.
html += "<img src = '"+val.imageLink + "'" +"alt='" + val.altText + "'>";
// Only change code above this line.
html += "</div>";
});
$(".message").html(html);
});
});
});
</script>
<div class="container-fluid">
<div class = "row text-center">
<h2>Cat Photo Finder</h2>
</div>
<div class = "row text-center">
<div class = "col-xs-12 well message">
The message will go here
</div>
</div>
<div class = "row text-center">
<div class = "col-xs-12">
<button id = "getMessage" class = "btn btn-primary">
Get Message
</button>
</div>
</div>
</div>
$.getJSON("/json/cats.json", function(json) {
内添加json = json.filter(function(val){
return(val.id !== 1 );
})
<script>
$(document).ready(function() {
$("#getMessage").on("click", function() {
$.getJSON("/json/cats.json", function(json) {
var html = "";
// Only change code below this line.
json = json.filter(function(val){
return(val.id !== 1 );
})
// Only change code above this line.
json.forEach(function(val) {
html += "<div class = 'cat'>"
html += "<img src = '" + val.imageLink + "' " + "alt='" + val.altText + "'>"
html += "</div>"
});
$(".message").html(html);
});
});
});
</script>
<div class="container-fluid">
<div class = "row text-center">
<h2>Cat Photo Finder</h2>
</div>
<div class = "row text-center">
<div class = "col-xs-12 well message">
The message will go here
</div>
</div>
<div class = "row text-center">
<div class = "col-xs-12">
<button id = "getMessage" class = "btn btn-primary">
Get Message
</button>
</div>
</div>
</div>
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
$("#data").html("latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude);
});
}
<script>
// Only change code below this line.
if (navigator.geolication){
navigator.geolocation.getCurrentPosition(function(position){
$("#data").html("latitude+" + position.coords.latitude +"<br>longitude:"+position.coords.longitude);
});
}
// Only change code above this line.
</script>
<div id = "data">
<h4>You are here:</h4>
</div>
JSON
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成(网络传输速度)。
Ajax