【问题标题】:How to hide JSON data by creating a div如何通过创建 div 来隐藏 JSON 数据
【发布时间】:2015-03-08 06:09:47
【问题描述】:

为了隐藏 JSON 数据(以便以后可以淡入),我修改了 a1.start 函数,创建了名为“加载”的 div,但在 Chrome 的 Javascript 控制台中,有

Uncaught SyntaxError: Unexpected token ILLEGAL

在 HTML 中:a1.start($("#cakeHook"),"a1data.json");

<!doctype html>
<html>
    <head>
    <style>
    </style>
    <title>Cake Baby Bakery</title>
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
    <script src="a1.js"></script>
    <link href="css/bootstrap.css" rel="stylesheet"/>
    <link href="css/basica1.css" rel="stylesheet"/>
    </head>
    <body>  
        <div id="cakeHook" ></div>
        <script>

        a1.start($("#cakeHook"),"a1data.json");

        </script>   
    </body>
</html>

a1.start

a1.start = function(hookElementSelection, dataurlForJsonFile) {

jQuery('<div/>', {
id: 'loading',
text: 'Loading...'
}).appendTo('hookElementSelection');

$('hookElementSelection).hide();

    a1.products = {};
    a1.recipes = {};
    a1.suppliers = {};
    a1.bakedRecipes = [];

    //make an ajax call and wait for success
    $.ajax({url:dataurlForJsonFile}).success(function(data) {

        //get the recipe data
        parseJSONData(data);

        //put the recipes on the page
        $.each(a1.recipes, function(i, recipe) {
            recipe.render(hookElementSelection);
        });
        renderCalculator(hookElementSelection); //add in the final calculation logic
    });
};

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    当您尝试使用变量时,您使用的是字符串:

    .appendTo('hookElementSelection')
    

    应该是:

    .appendTo(hookElementSelection)
    

    那么当你再次尝试使用该变量时,你有一个未终止的字符串:

    $('hookElementSelection).hide();
    

    应该是:

    hookElementSelection.hide();
    

    【讨论】:

      【解决方案2】:

      SO 上突出显示的语法表明您的错误。您缺少 ' 字符:

      $('hookElementSelection').hide();
      

      但是,您似乎已经在函数的第一个参数中传递了对 jQuery 对象的引用,因此用以下代码替换该行代码就足够了:

      hookElementSelection.hide();
      

      【讨论】:

        【解决方案3】:

        我认为在第 6 行和第 8 行中,您的 a1start.js 中有错误。改为:

        }).appendTo(hookElementSelection);
        
        hookElementSelection.hide();
        

        去掉单引号,hookElementSelection 是一个变量,是一个 jQuery 对象,不能用作字符串。

        【讨论】:

          猜你喜欢
          • 2015-03-25
          • 1970-01-01
          • 2016-11-24
          • 2019-07-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-08
          • 2017-10-18
          相关资源
          最近更新 更多