【问题标题】:Create dynamic id using purejs使用 purejs 创建动态 id
【发布时间】:2014-02-26 13:28:53
【问题描述】:

使用 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>

【问题讨论】:

    标签: javascript pure-js


    【解决方案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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-16
      • 2018-01-09
      • 2014-08-31
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      相关资源
      最近更新 更多