2015-10-17 87 views
-2

我想追加此項目的div,但它沒有工作......這裏是我的代碼:追加項目的div

$("document").ready(function(){ 
    var aMenu = [{ 
    title: "Home", 
    image: "/Icons/1.png", 
    items: [ 
     { title: "about", w: "x", h: "y", image: "/Images/1-1.PNG" }, 
     { title: "feature", w: "x", h: "y", image: "/Images/1-2.PNG" }, 
     { title: "themes", w: "x", h: "y", image: "/Images/1-3.PNG" }, 
     { title: "typography", w: "x", h: "y", image: "/Images/1-4.PNG" }, 
     { title: "look", w: "2x", h: "y", image: "/Images/1-5.PNG" } 
    ] 
    }]; 
    var dv = ("#dvTitleMenu"); 
    for (i = 0; i < aMenu.length; i++) { 
    $("#dvTitleMenu") 
    .append("<div class='col6'><a href='#'><h2>"+aMenu(i).title+"</h2></a></div>"); 
    } 
});   
+0

*什麼*沒有工作,怎麼樣?你有沒有得到任何錯誤?請您格式化您的代碼並解釋您的問題,最好包括指向小提琴或codepen的鏈接;現在,不可能知道你的問題是什麼。 –

+0

但有一點突出:'aMenu(i)'是無效的語法,你可能是指'aMenu [i]'。 –

回答

0

試試這個..

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script> 
<div id="dvTitleMenu"></div> 
<script> 
$("document").ready(function(){ var aMenu = [{ 
    title: "Home", image: "/Icons/1.png", 
    items: [{ title: "about", w: "x", h: "y", image: "/Images/1-1.PNG" }, 
    { title: "feature", w: "x", h: "y", image: "/Images/1-2.PNG" }, 
    { title: "themes", w: "x", h: "y", image: "/Images/1-3.PNG" }, 
    { title: "typography", w: "x", h: "y", image: "/Images/1-4.PNG" }, 
    { title: "look", w: "2x", h: "y", image: "/Images/1-5.PNG" }] 
    }] 
    var dv = ("#dvTitleMenu"); 
    for (i = 0; i < aMenu.length; i++) { 
    //console.log(aMenu.items.length); 
    console.log(aMenu[i].items); 
    $("#dvTitleMenu").append("<div class='col6'><a href='#'> <h2> " +aMenu[i].title + "</h2></a></div> "); 



    } });  
    </script> 

http://js.do/code/70795

+0

謝謝......它工作:))) – mozhii