利用thymeleaf动态拼接HTML标签的属性

需求背景

最近在做的考试系统,进行到考试模块,该模块逻辑大致如下:从后台查询到各类型的题目数据,然后将数据放入前台模板中,生成一张试卷。前端使用的是HTML+ thymeleaf。在做这个功能时,遇到下列需求:
每一道题目的题干和选项,都有特定的id,这些id将会在提交答案和检测是否答题的功能中使用到。这就要保证每一个id都不同,因此,对于每一道题,我们都需要动态的拼接id,这样就可以保证每一道题的id都不同。而获取后台数据我才用的是thymeleaf循环遍历的 th:each

代码实现

这里放入试题的模板,可以看到,我通过==th:each=“single,singleStart: ${single}”==的语法拿到了后台保存的单选题List,并对它循环取值
在li标签中,我的id写法为:th:id="‘qu_0_’ + ${singleStart.index}",通过th语法,实现了对id属性的动态拼接,这样就保证了id不重复且和每道题有关联

<div class="test_content_nr" th:each="single,singleStart: ${single}">
                            <ul>
                                <li th:id="'qu_0_' + ${singleStart.index}">
                                    <div class="test_content_nr_tt">
                                        <i th:text="${singleStart.count}">1</i><font th:text="${single.quesTitle}">在生产管理信息系统中,下列操作步骤能正确将工单推进流程的是(  )</font><b class="icon iconfont">&#xe881;</b>
                                    </div>

                                    <div class="test_content_nr_main">
                                        <ul>
                                            <li class="option">
                                                <input type="radio" class="radioOrCheck" th:name="'answer' + ${singleStart.count}"
                                                       th:id="'0_answer_'+ ${singleStart.count} +'_option_1'" />
                                                <label th:for="'0_answer_'+ ${singleStart.count} +'_option_1'">
                                                    A.
                                                    <p class="ue" style="display: inline;" th:text="${single.answerContent['A']}" >在失败页面重新发起</p>
                                                </label>
                                            </li>
                                            <li class="option">
                                                <input type="radio" class="radioOrCheck" th:name="'answer' + ${singleStart.count}"
                                                       th:id="'0_answer_'+ ${singleStart.count} +'_option_2'" />
                                                <label th:for="'0_answer_'+ ${singleStart.count} +'_option_2'">
                                                    B.
                                                    <p class="ue" style="display: inline;"th:text="${single.answerContent['B']}">在缺陷单界面中点击“推进流程”按钮</p>
                                                </label>
                                            </li>
                                            <li class="option">
                                                <input type="radio" class="radioOrCheck" th:name="'answer' + ${singleStart.count}"
                                                       th:id="'0_answer_'+ ${singleStart.count} +'_option_3'" />
                                                <label th:for="'0_answer_'+ ${singleStart.count} +'_option_3'">
                                                    C.
                                                    <p class="ue" style="display: inline;" th:text="${single.answerContent['C']}">在缺陷单界面中点击“提交”按钮</p>
                                                </label>
                                            </li>
                                            <li class="option">
                                                <input type="radio" class="radioOrCheck" th:name="'answer' + ${singleStart.count}"
                                                       th:id="'0_answer_'+ ${singleStart.count} +'_option_4'" />
                                                <label th:for="'0_answer_'+ ${singleStart.count} +'_option_4'">
                                                    D.
                                                    <p class="ue" style="display: inline;" th:text="${single.answerContent['D']}">后台启动流程推进</p>
                                                </label>
                                            </li>
                                        </ul>
                                    </div>
                                </li>
                            </ul>
                        </div>

总结

th的语法功能十分强大,我也是才刚刚使用不久,还有更多强大的功能等我去探索,和大家一起学习

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在Thymeleaf中,可以使用路径拼接来引用其他页面或资源。路径拼接可以通过使用Thymeleaf的内置表达式来实现。例如,如果你想引用一个名为"header.html"的页面,可以使用以下语法进行路径拼接: ```html <div th:include="~{templates/header.html}"></div> ``` 在这个例子中,"templates/header.html"是相对于当前页面的路径。使用"~{}"来告诉Thymeleaf进行路径拼接。这样,Thymeleaf会根据当前页面的路径来拼接正确的路径。 另外,你也可以使用Thymeleaf的URL表达式来进行路径拼接。例如,如果你想引用一个名为"style.css"的样式表,可以使用以下语法: ```html <link th:href="@{/static/css/style.css}" rel="stylesheet" /> ``` 在这个例子中,"@{}"告诉Thymeleaf进行URL路径拼接。"/static/css/style.css"是相对于项目根目录的路径。 总结起来,Thymeleaf提供了多种方式来进行路径拼接,你可以根据具体的需求选择适合的方式来引用其他页面或资源。 #### 引用[.reference_title] - *1* *2* *3* [thymeleaf js 动态拼接html_Thymeleaf学习](https://blog.csdn.net/weixin_39775896/article/details/110301497)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值