我的JavaScript无法正常工作 [英] My javascript won't work as it should

查看:160
本文介绍了我的JavaScript无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用HTML创建一个网站,该网站使用javascript接收JSON格式的数据,然后它将这些数据注入到我的网站中.

I'm trying to create a website in HTML which recieves JSON-formatted data using javascript and, then it will be injecting that data into my website.

问题是我找不到任何错误.我使用了示例代码.将数据从我的网络服务器获取到我的HTML中.我使用上面链接的示例建立了到数据库的连接.但后来我想将现有代码移到此示例中

The problem is I can't find any errors. I used this example code. To get data from my webserver into my HTML. I established a connection to my database using a example linked above. But then I wanted to move that existing code into this example over here.

在上面的示例中,我已链接我的javascript文件,该文件获取数据并使用id创建一个div,该id与在获取数据的javascript文件中分配的id相同.但这根本不起作用.在将要填充数据的div中,它为空.

In the example above, I've linked my javascript file which fetches the data and creates a div with the a id, the same id which is assigned in the javascript file that fetches the data. But it won't work at all. In the div that would be filled with data, is just empty.

fetch.js

$(document).ready(function(){
    var output = $('#output');

    $.ajax({
        url: 'http://samcroft.co.uk/demos/updated-load-data-into-phonegap/landmarks.php',
        dataType: 'jsonp',
        jsonp: 'jsoncallback',
        timeout: 5000,
        success: function(data, status){
            $.each(data, function(i,item){ 
                var landmark = '<h1>'+item.name+'</h1>'
                + '<p>'+item.latitude+'<br>'
                + item.longitude+'</p>';

                output.append(landmark);
            });
        },
        error: function(){
            output.text('There was an error loading the data.')
        }
    });
});

这是一个实时演示,代码在Web服务器上联机时正在加载.但是不是本地的.是什么原因造成的?

Here's a live demo, the code is loading when it's online on a webserver. But not when it's local. What is causing this?

http://codele.se/app/flip/debatt.html

推荐答案

在您的网站上使用Firebug时,我发现您的html包含更新的数据,如下所示:

On using firebug on your site i found out that your html contains updated data as shown here:

问题是您正在使用背景图片,该图片在从您的网站中删除以下代码行时会隐藏起来:

The problem is you are using background image that hides it on removing this line of code from your site:

<div style="z-index: 3; left: 512px; transform: rotateY(0deg);" class="page cover"> 

我得到以下输出:

仔细观察后,我发现您在html中有两个具有相同ID输出的div,因此请确保只有一个div具有output id bcoz ID在html中应该是唯一的.

on having a closer look I found that you have two div in html that have the same ID output, so make it sure that only one div has the output id bcoz ID should be unique in html.

这篇关于我的JavaScript无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆