[英]Thymeleaf and javascript
I trying display html
template with thymeleaf
loop like: 我试图用thymeleaf
循环显示html
模板,例如:
<th:block th:each="ticket : ${tickets}">
<div th:id="'ticket_' + ${ticket.ticketId}" th:include="${ticket.templateName}"></div>
<div th:id="${ticket.ticketId}" th:if="${#strings.toString(ticket.ticketStatus)} == 'CANCELED'" th:include="'canceled'"></div>
</th:block>
If ticketStatus == 'CANCELED'
I include html with js code which alert parent ID and its ID different for all elements. If ticketStatus == 'CANCELED'
我将html和js代码包括在内,以警告父ID及其所有元素的ID不同。 But JS alert same id (id of first element). 但是JS警报具有相同的ID(第一个元素的ID)。 Why so? 为什么这样? UPD: JavaScript part: UPD:JavaScript部分:
<script class='idForScript' th:inline="javascript">
/*<![CDATA[*/
var id = $('.idForScript').parent().attr('id');
alert(id);
$('#ticket_'+id).find('.sortable').prepend('<div style="z-index: 232; background: grey; color:red; width:100%; height:100%; opacity:0.9; text-align:center; position: absolute;"><span style="position: absolute; margin-left: auto; margin-right: auto;left: 0; right: 0; top:35%;"><h1 style="font-weight:900;">YOUR TICKET WAS CANCELED</h1></span></div>');
/*]]>*/
</script>
You can do this by skipping the include part quite easily. 您可以通过轻松地跳过包含部分来完成此操作。 This is all in one file now. 现在全部放在一个文件中。 Your script: 您的脚本:
<script th:inline="javascript">
/*<![CDATA[*/
function yourFunction(id){
alert(id);
// Rest of your code.
}
/*]]>*/
</script>
Inside your iteration: 在您的迭代中:
<div th:id="${ticket.ticketId}" th:if="${#strings.toString(ticket.ticketStatus)} == 'CANCELED'">
<script th:inline="javascript">
/*<![CDATA[*/
$(function() {
yourFunction([[${ticket.ticketId}]]);
});
/*]]>*/
</script>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.