2014-02-26 57 views
0

使用purejs,我想爲每個渲染元素創建動態ID。使用purejs創建動態ID

在這段代碼中,我需要爲每個標籤設置id。這個'a'標籤將創建依賴於jsonData。

<!-- HTML template --> 
    <div id="template" class="template"> 
    Hello <a></a> 
    </div> 
    <!-- result place --> 
    <div id="display" class="result"></div> 

<script> 
    var jsonData = {data:[{who:'Google!',site:'http://www.google.com/'},{who:'Yahoo!',site:'http://www.yahoo.com/'}]}; 

    //select the template 
    $('#template') 

     //map the HTML with the JSON data 
     .directives({ 
     'div a':{ 
      'd<-data':{ 
       '.':'d.who', 
       '@href':'d.site' 
      } 
     } 
     }) 

     //generate the new HTML 
     .render(jsonData) 

     //place the result in the DOM, using any jQuery command 
     .appendTo('#display'); 

    </script> 

回答

1

設置動態ID使用「.pos」標記。這個「.pos」將數據的位置放在一個數組中。

//select the template 
$('#template') 

//map the HTML with the JSON data 
.directives({ 
    'div a':{ 
     'd<-data':{ 
      '@id':'xyz_#{d.pos}', 
      '.':'d.who', 
      '@href':'d.site' 
     } 
    } 
}) 

//generate the new HTML 
.render(jsonData) 

//place the result in the DOM, using any jQuery command 
.appendTo('#display');