/////
//credit to @Aditya from StackOverflow for helping me edit this code
$(document).ready(function() {
$('#search').on('click', function(){
$.ajax({
dataType: 'jsonp', //data in jsonp
contentType: "application/json; charset=utf-8",
url: 'http://live.nhle.com/GameData/RegularSeasonScoreboardv3.jsonp',
jsonpCallback: 'loadScoreboard',
success: function (data) {
var text = data['games'];
console.log(text);
var str = '';
var textsearch = $('#text').val();
if(textsearch!=null)
{
str = '';
var text = data.games;
text = text.filter(function (game) {
return game.tsc !== '';
});
for (var key in text)
{
if(text[key].hasOwnProperty('htn') && text[key].hasOwnProperty('hts'))
{
if(text[key]['htn'].toLowerCase().indexOf(textsearch.toLowerCase()) != -1)
{
str= capitalize(text[key]['ts']) + ", " + text[key]['htn']+ " vs "+ text[key]['atn']+ " score : "+ text[key]['hts']+"-"+text[key]['ats']+ ";" + "\n";
}
if( text[key]['atn'].toLowerCase().indexOf(textsearch.toLowerCase())!=-1)
str += capitalize(text[key]['ts']) + ", " + text[key]['atn'] + " vs "+ text[key]['htn'] + " score : "+ text[key]['ats']+"-"+text[key]['hts'] + ";" + "\n";
}
}
console.log("str: " + str);
$('#result').html('<p>' + str + '</p>');
$('#result').css({'color': '#FF5050'});
$('#result').css({'text-align': 'center'});
$('#result').css({'font-size': '20px'});
$('#result').css({'font-family': 'Georgia'});
}
}
});
});
});
function capitalize (str) {
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}
我在那里有这个代码,我想这样,当我们说,一个团队赢了三个,它说,&#34;多么井喷!&#34;如果一个团队一个人赢了,那么就会说出像#34;多么痛苦的指甲!&#34;我想要一个非常相似的格式,但不是现在粘贴到页面的内容,我想要这些短语。任何帮助将不胜感激!
答案 0 :(得分:1)
一个简单的switch语句可以解决问题。
var output;
switch(score){//score is a variable which hold the difference
case 1:
output = "What a nail biter!";
break;
case 3:
output = "What a blowout!";
break;
default:
output = "What a match!";
}
$('#result').html('<p>' + output + '</p>');
答案 1 :(得分:0)
不会像在文本[key] ['hts'] - text [key] ['ats']上进行减法一样简单,如果差值为正或负3,则附加到str变量“什么是井喷”?对我来说,建议的最重要的是你在这个得分显示的位置,在得分旁边?