如何在速度模板中的for循環內為div生成唯一的類名?

[英]How can I generate unique class name for div inside a for loop in velocity template?


I have a for loop inside my velocity template to display element on web page. I am wrapping them inside div but I end up having same div name for all the elements. How can I make sure that all the div is having unique class name for all the elements.

我在我的速度模板中有一個for循環來在網頁上顯示元素。我將它們包裝在div中但我最終為所有元素都有相同的div名稱。如何確保所有div都具有所有元素的唯一類名。

    #foreach($i in [0..$size])
        <div class= "styles">
             <span class="actor">$discoverActorConfs.get($i).actor</span>
             <span class="remove aui-icon aui-icon-small aui-iconfont-    remove" title="Remove project" data key="$discoverActorConfs.get($i).actor" onclick="transit_time_remove_proj('$discoverActorConfs.get($i).actor')">
             </span>

        </div>

So that I can have unique class name while using it inside script tag.

這樣我在腳本標記中使用它時可以擁有唯一的類名。

<script>

    $(document).ready(function() {


            $('div.new-div, div.styles').connections();

            var connections = $('connection, inner');
            setInterval(function() { connections.connections('update') }, 100);
        });
    </script>

1 个解决方案

#1


2  

Having unique classes is sort of clashing with the purpose of having classes. you should rather have unique IDs for each div. However if you want to generate unique classes,

擁有獨特的類有點與類的目的沖突。你應該為每個div都有唯一的ID。但是,如果要生成唯一的類,

#foreach($i in [0..$size])
        <div class= "styles-$i">
             <span class="actor">$discoverActorConfs.get($i).actor</span>
             <span class="remove aui-icon aui-icon-small aui-iconfont-    remove" title="Remove project" data key="$discoverActorConfs.get($i).actor" onclick="transit_time_remove_proj('$discoverActorConfs.get($i).actor')">
             </span>
      </div>

If you pick to go with unique IDs, you could do,

如果你選擇使用唯一ID,你可以這樣做,

#foreach($i in [0..$size])
            <div class= "styles" id="div-$i">
                 <span class="actor">$discoverActorConfs.get($i).actor</span>
                 <span class="remove aui-icon aui-icon-small aui-iconfont-    remove" title="Remove project" data key="$discoverActorConfs.get($i).actor" onclick="transit_time_remove_proj('$discoverActorConfs.get($i).actor')">
                 </span>
    </div>

All you got to do is append -$i which makes sure that ID/class names are unique

你所要做的就是附加 - $ i,它確保ID /類名稱是唯一的


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/07/13/472a2dd9787c885111dff515a6e41e03.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com