동적으로 그려진 html 각 요소들을, 추후 사용자의 동작에 맞춰 컨트롤하고 싶을때가 있습니다.
이런 경우 각 태그에 attribute를 부여해놓으면 쉽게 컨트롤 할 수 있습니다.
<div th:attr="data-el_id=${element.getId()}">
el_id에 element.getId() 값을 세팅하는 코드입니다.
<div th:attr="data-id=${element.getId()},data-name=${element.getName()}">
태그 안에서 여러개 속성을 동시에 부여할 수도 있습니다.
$("li").data("el_id");
jQuery를 활용하여 설정한 attribute에 쉽게 접근할 수 있습니다.
참고 1 : https://stackoverflow.com/questions/24411826/using-data-attribute-with-thymeleaf
using data-* attribute with thymeleaf
Can I set data-* attribute with thymeleaf? As I understood from thymeleaf documentation I tried: <div th:data-el_id="${element.getId()}"> <!-- doesn't work --> <div data-th-el_id="${
stackoverflow.com
참고 2 : https://stackoverflow.com/questions/4145636/jquery-custom-attributes
Jquery custom attributes
I'm thinking about using custom attributes in Jquery to avoid using class or id attributes, to not interfere with the html designers. Taking into account this idea, the html should be pieces like:...
stackoverflow.com
'Thymeleaf' 카테고리의 다른 글
[Thymeleaf] 배열 데이터 each 없이 한번에 표현 (0) | 2022.08.19 |
---|---|
[Thymeleaf] 스프링 모델 값을 자바스크립트에서 사용하기 (0) | 2022.08.11 |