简体   繁体   English

Javascript显示图像

[英]Javascript displaying image

my overall goal is to load an image given user lattitude and longitude. 我的总体目标是根据用户的经度和纬度加载图像。 However, my code has an error logic somewhere I assume (document.write("Dog") is never displaying to the screen) so I have to assume something is going wrong. 但是,我的代码在我假设的某个地方有一个错误逻辑(document.write(“ Dog”)从未显示在屏幕上),因此我必须假设某些地方出了问题。 The user is being prompted for a lat and longitude after clicking the button but afterwards nothing is happening. 单击按钮后,提示用户输入纬度和经度,但此后未发生任何事情。 Any Ideas what might be going on? 任何想法可能会发生什么? I'm a beginner so i am sure it is something simple. 我是一个初学者,所以我确定这很简单。

<body>
<p>Click the button to demonstrate the prompt box.</p>
<button onclick="myFunction()">Try it</button>
   <script>
        function myFunction() {
            var x;
            var latittude = prompt("Please enter a lat");
            var longitude = prompt("please enter your longitude");
            var img = document.createElement('img');
            img.src = 'http://maps.googleapis.com/maps/api/streetview?size=400x400&location='+ latittude + ',' + longitude + '&heading=235&sensor=false';
            img.height = 200;
            img.width = 300;
            img.alt = alt;
            document.write(img.src);
            document.write("dog");
            document.getElementById('body').appendChild(img);
        }
</script>
 </body>

img.alt = alt; but you have not defined a variable named alt . 但您尚未定义名为alt的变量。

document.getElementById('body') but there is no id with a value of 'body' , to reference it use document.body.appendChild(img); document.getElementById('body')但没有id'body' id ,要使用document.body.appendChild(img);进行引用document.body.appendChild(img); .

Also unless this is just messing around for practice, take a look at: Why is document.write considered a “bad practice”? 另外,除非这只是为了练习而已,否则请看看: 为什么document.write被认为是“不良做法”? .

For future reference if you use the developer tools in your browser (F12) any script errors will be printed out in the console . 供以后参考,如果您在浏览器(F12)中使用开发人员工具,则任何脚本错误都将在控制台中打印出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM