需要不可数循环dillema的逻辑解决方案(Need logical solution for uncountable loop dillema)

编程入门 行业动态 更新时间:2024-10-26 16:33:23
需要不可数循环dillema的逻辑解决方案(Need logical solution for uncountable loop dillema)

假设我在java中有这种循环(不是一个确切的代码,它既不是真正有效的代码,只是为了给你一个通用的情况视角):

print "<ul>"; while (res = fetch(database)) { print ("<li>" + res.col['data'] + "</li>"); } print "</ul>";

我有这个CSS,这使得列表中的最后一项具有红色。

ul li:last-child { color: red; }

这适用于大多数浏览器。 问题是:

我也需要在IE8上做这个工作。 IE8不支持last-child。 我无法测试“while”的当前迭代是否进入最后一次迭代。 或者说,我无法检查循环何时结束。 这不是一个普通的循环,但让我们这样说。 所以我不能给最后一个<li>一个班级,比如class="lastchild" 。 我也试过javascript和jquery,两者都不能选择“last-child”,因为它们取决于选择的css,我想。

在这种情况下,最好的方法是怎样的? 谢谢。

Supposed I have this kind of loop in java (not an exact code, neither it's a truly valid code, but just to give you a general perspective of situation):

print "<ul>"; while (res = fetch(database)) { print ("<li>" + res.col['data'] + "</li>"); } print "</ul>";

and I have this CSS, which makes the last item on the list has red color.

ul li:last-child { color: red; }

this works fine on most browsers. the problems are:

I need to make this work on IE8 too. IE8 doesn't support last-child. I cannot test whether current iteration of "while" is entering last iteration or not. or let's say, there's no way I could check when the loop would end. this is not an ordinary loop, but let's just say like that. so I can't give the last <li> a class, say class="lastchild". I also tried with javascript and jquery, and both can't select "last-child" either, as they depends on the css for the selection, I think.

how is the best approach in this situation? thanks.

最满意答案

print "<ul>"; String liText=null; while (res = fetch(database)) { if (liText!=null) { print ("<li>" + liText + "</li>"); } liText=res.col['data']; } if (liText!=null) { print ("<li style='the last element style'>" + liText + "</li>"); } print "</ul>";

移动最后一行的添加,然后在那里应用所需的样式

print "<ul>"; String liText=null; while (res = fetch(database)) { if (liText!=null) { print ("<li>" + liText + "</li>"); } liText=res.col['data']; } if (liText!=null) { print ("<li style='the last element style'>" + liText + "</li>"); } print "</ul>";

Move the adding of the last line out of the while and apply desired style there

更多推荐

本文发布于:2023-07-31 05:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1341887.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:逻辑   解决方案   不可数   dillema   loop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!