地理位置跟踪getElementById不起作用

时间:2016-03-30 07:24:02

标签: javascript jquery

我正在通过freecodecamp进行挑战,任务是显示当地天气,因此我必须得到用户的位置。我可以获得位置from here但是在打印之后我试图获取div的getElementById,我使用JS打印,其中null作为响应。我想获得键值对,以便我可以用它们做些什么。这是我的代码:



<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <title>Location Trace | freecodecamp Challanges</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>
    <body>
        <div id="GeoResults"></div>
        <script>
        $.getJSON("http://ip-api.com/json/?callback=?", function(data) {
            var table_body = "";
            var count = 0;
            $.each(data, function(k, v) {
				//JSON.stringify(j); // '{"name":"binchen"}'
				
				table_body += '<div id=Coun_'+count+'>'+v+'</div>';
				
                //table_body += "<tr><td id='FC_"+count+"'>" + k + "</td><td><b id='SC_"+count+"'>" + v + "</b></td></tr>";
                count++;
            });
            $("#GeoResults").html(table_body);
        });
        </script>
        <script> 
			var x = document.getElementById('Coun_1') /*= 'Dooone!!!'*/;
			console.log(x);
        </script>
    </body>
</html>
&#13;
&#13;
&#13;

提前致谢!

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <title>Location Trace | freecodecamp Challanges</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>
    <body>
        <div id="GeoResults"></div>
        <script>
        $.getJSON("http://ip-api.com/json/?callback=?", function(data) {
            var table_body = "";
            var count = 0;
            $.each(data, function(k, v) {
				//JSON.stringify(j); // '{"name":"binchen"}'
				
				table_body += '<div id=Coun_'+count+'>'+v+'</div>';
				
                //table_body += "<tr><td id='FC_"+count+"'>" + k + "</td><td><b id='SC_"+count+"'>" + v + "</b></td></tr>";
                count++;
            });
            $("#GeoResults").html(table_body);
            var x = document.getElementById('Coun_1').innerHTML; /*= 'Dooone!!!'*/;
			console.log(x);
        });
        </script>
    </body>
</html>
&#13;
&#13;
&#13;