从HTML中的外部JS文件调用函数

时间:2015-03-22 15:36:53

标签: javascript jquery html function

我似乎无法使其正常工作,我尝试移动它,尝试创建一个按钮来执行此操作,但我无法让它获取数据。

是否有加载javascripts的顺序?我忘了包含一些东西吗?我试过把它从http://jsfiddle.net/运出来,似乎在那里工作,但我不知道我是如何在网站上运行的。

function summonerLookUp() {
    var ID = "Nightblue3"; //ingame name of a player  
    var YOURKEY = "My_API_Key" //can't share my key
    if (ID !== "") {

        $.ajax({
            url: 'https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/' + ID + '?api_key=' + YOURKEY,
            type: 'GET',
            dataType: 'json',
            data: {

            },
            success: function (json) { //gets info
                var userID = ID.replace(" ", "");
                userID = userID.toLowerCase().trim(); //sends it to lowercase
                summonerName = json[userID].name;
                summonerLevel = json[userID].summonerLevel;
                summonerID = json[userID].id;
                summonerDate = json[userID].revisionDate;
                summonerIcon = json[userID].profileIconId; 
                document.getElementById("sName").innerHTML = summonerName;
                document.getElementById("sLevel").innerHTML = summonerLevel;
                document.getElementById("sID").innerHTML = summonerID;
                document.getElementById("sDate").innerHTML = summonerDate;
                document.getElementById("sIcon").innerHTML = summonerIcon;


            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("error"); //if something is incorrect
            }
        });
    } else {
    }
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script language="JavaScript" type="text/javascript" src="jquery-2.1.3.min.js"></script>
    <script type="text/javascript" language="javascript" src="Script1.js"></script>
    <script>onload = function () { summonerLookUp(); }</script>
    <button onclick="summonerLookUp;">Click</button>
    
	
    <br/>Summoner Name: <span id="sName"></span>
    <br/>Summoner Level: <span id="sLevel"></span>	
    <br/>Summoner ID: <span id="sID"></span>
    <br/>Summoner Date: <span id="sDate"></span>
    <br/>Summoner Icon: <span id="sIcon"></span>


</body>
</html>

<br/>Summoner Name: <span id="sName"></span>
<br/>Summoner Level: <span id="sLevel"></span>     
<br/>Summoner ID: <span id="sID"></span>
<br/>Summoner Date: <span id="sDate"></span>
<br/>Summoner Icon: <span id="sIcon"></span>

`

1 个答案:

答案 0 :(得分:0)

我修好了!

我所要做的就是在HTML的部分添加<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>