我在Web Inspector中收到错误,如下所示:
TypeError: 'null' is not an object (evaluating 'myButton.onclick = function() {
var userName = myTextfield.value;
greetUser(userName);
return false;
}')
Run Code Online (Sandbox Code Playgroud)
这是我的代码(HTML):
<h2>Hello World!</h2>
<p id="myParagraph">This is an example website</p>
<script src="js/script.js" type="text/javascript"></script>
<form>
<input type="text" id="myTextfield" placeholder="Type your name" />
<input type="submit" id="myButton" value="Go" />
</form>
Run Code Online (Sandbox Code Playgroud)
这是JS:
var myButton = document.getElementById("myButton");
var myTextfield = document.getElementById("myTextfield");
function greetUser(userName) {
var greeting = "Hello " + userName + "!";
document.getElementsByTagName ("h2")[0].innerHTML = greeting;
}
myButton.onclick = function() {
var userName = …
Run Code Online (Sandbox Code Playgroud) 大家好,我有以下代码:
var map;
var infowindow;
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(initialize);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function initialize(position) {
var pyrmont = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: 500,
types: ['restaurant']
};
...
Run Code Online (Sandbox Code Playgroud)
基本上,如果我设置 Long/Lat 坐标,我可以让地图工作得很好,但我希望通过获取用户位置来传递这些坐标。使用上面的代码,地图显示在我的手机上,但出现以下错误:
TypeError: 'undefined' is not an object (evaluating 'position.coords.latitude')
Run Code Online (Sandbox Code Playgroud)
但在桌面上我没有得到地图和以下错误:
Uncaught TypeError: Cannot read property 'latitude' of undefined
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒我是 Javascript 和这些 API 的新手