如何在速度模板中的for循环内为div生成唯一的类名?

时间:2023-05-12 17:43:07

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 *ing 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 /类名称是唯一的

#1


2  

Having unique classes is sort of *ing 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 /类名称是唯一的