Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript显示图像_Javascript - Fatal编程技术网

Javascript显示图像

Javascript显示图像,javascript,Javascript,我的总体目标是加载给定用户纬度和经度的图像。然而,我的代码在我假设的某个地方有一个错误逻辑(document.write(“Dog”)从未显示在屏幕上),所以我必须假设出了问题。单击按钮后,将提示用户输入纬度和经度,但之后什么也没有发生。你知道会发生什么吗?我是一个初学者,所以我相信这很简单 <body> <p>Click the button to demonstrate the prompt box.</p> <button onclick="myF

我的总体目标是加载给定用户纬度和经度的图像。然而,我的代码在我假设的某个地方有一个错误逻辑(document.write(“Dog”)从未显示在屏幕上),所以我必须假设出了问题。单击按钮后,将提示用户输入纬度和经度,但之后什么也没有发生。你知道会发生什么吗?我是一个初学者,所以我相信这很简单

<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>

单击按钮以演示提示框

试试看 函数myFunction(){ var x; var latittude=提示(“请输入lat”); var经度=提示(“请输入您的经度”); var img=document.createElement('img'); img.src=http://maps.googleapis.com/maps/api/streetview?size=400x400&location=“+纬度+”、“+经度+”&航向=235&传感器=false”; img.高度=200; img.width=300; img.alt=alt; 文件编写(img.src); 文件。写(“狗”); document.getElementById('body').appendChild(img); }
img.alt=alt
但您尚未定义名为
alt
的变量

document.getElementById('body')
,但没有值为
'body'
id
,要引用它,请使用
document.body.appendChild(img)

另外,除非这只是为了练习而胡闹,请看:


如果您在浏览器(F12)中使用开发人员工具,任何脚本错误都将在控制台中打印出来,以供将来参考。

不要使用
文档。编写
,请参阅中的警告。请改用DOM方法。谢谢。根据这个算出了!而且,我对开发人员工具完全不了解。这将使学习过程更加容易。