2015-03-13 70 views
0

我想做一個非常簡單的html tableTree。我沒有任何有關html/javascript編程的經驗,所以我遍歷谷歌找到我試圖實現的例子。Uncaught TypeError jqute

我目前正在試圖找到一個簡單的方法來傳遞一個json文件到一個html文檔中,並且我通過執行代碼部分我自己並使用ajax和jquery已經取得了成功。

但是我發現了一個使用jqote2的例子,雖然實現這個例子給了我一個錯誤「Uncaught TypeError:undefined不是一個函數」。我想我失去了一些東西,雖然我想不出什麼,所以我希望我能得到一些在這裏:)援助

<!DOCTYPE html> 
<html> 
<head> 
    <script src="jquery/jquery-1.11.2.min.js"></script> 
    <script src="jqote2/jquery.jqote2.js"></script> 
    <script type="text/html" id="template"> 
    <![CDATA[ 
    <tr> 
     <td class="no"><%= j+1 %></td> 
     <td class="title"><%= this.Title %></td> 
    </tr> 
    ]]> 
    </script> 
    <!-- 2 load the theme CSS file --> 
    <title>Test</title> 
    </head> 

    <body> 
    <script type="text/javascript"> 
    var jsondata = [{"Title": "Dr.", "Surname": "House", "Firstname": "Geronimo"},{"Title": "Mr.", "Surname": "Franklin", "Firstname": "Benjamin"}]; 
    // Now call jQote on the template providing your json data 
    $('#template').jqote(jsondata).appendTo($('#users')); 
    </script> 

    <div> 
    <table id="users"></table> 
    </div> 

</body> 
</html> 

我已經基於在http://aefxx.com/jquery-plugin/jqote/

發現的例子建立這個代碼當運行此代碼我得到錯誤的

$('#template').jqote(jsondata).appendTo($('#users')); 

所以我失去了什麼:)我已經檢查幷包含文件確實存在和路徑是正確的。

回答

0

您在CData部分有問題 像「<」和「&」這樣的字符在XML元素中是非法的。

「<」將產生錯誤,因爲解析器將其解釋爲新元素的開始。

「&」將產生錯誤,因爲解析器將其解釋爲字符實體的開始。

某些文本(如JavaScript代碼)包含大量「<」或「&」字符。爲了避免錯誤,腳本代碼可以定義爲CDATA。

解析器忽略CDATA部分內的所有內容。 moveover像你一樣

http://www.w3schools.com/xml/xml_cdata.asp

從JSON對象繪製一個表格會是這樣的

<!DOCTYPE html> 
<html> 
<head> 
    <script src="jquery-1.11.2.min.js"></script> 

    <!-- 2 load the theme CSS file --> 
    <title>Test</title> 
    </head> 


    <script type="text/javascript"> 
    var jsondata = [{"Title": "Dr.", "Surname": "House", "Firstname": "Geronimo"},{"Title": "Mr.", "Surname": "Franklin", "Firstname": "Benjamin"}]; 
    // Now call jQote on the template providing your json data 

    $(document).ready(function() { 
     $.each(jsondata, function(key, value) { 
      $.each(value, function(k, v) { 
        $("#"+k).html(v); 

      }); 
    }); 
}); 

    </script> 

    <body> 
    <table> 
    <tr> 
    <td id="Title"></td> 
    <td id="Surname"></td> 
    <td id="House"></td> 
    <td id="Firstname"></td> 
    </tr> 
    </table> 

</body> 
</html> 

第一循環將削減對象爲數組,你可以不寫字符串,第二個一個會讓你得到的鍵和值,然後做你想要的東西^ _ ^希望這可以幫助

+0

我們在這裏談論什麼解析器因爲我明白它

相關問題