TechScribe Notes
Json파일연결해서 조건에 맞는 이미지 파일 가져오기 본문
국가코드와 이름을 연결한 json 파일
function TagImages() {
$.getJSON('/assets/js/country.JSON', function (data) {
// JSON 데이터가 올바른지 확인
if (data && Array.isArray(data.countries)) {
const countryMap = {};
data.countries.forEach(function (country) {
countryMap[country['한글명']] = country.ISO2;
});
$('.tag.origin').each(function () {
const countryName = $(this).find('span').text().trim();
const iso2Code = countryMap[countryName];
if (iso2Code) {
const imgSrc = `/assets/image/flags/${iso2Code}.svg`;
$(this).find('img').attr('src', imgSrc);
}
});
} else {
console.error('Invalid JSON structure:', data);
}
}).fail(function () {
console.error('Error loading JSON file.');
});
}
'project > jquery' 카테고리의 다른 글
일정 너비 이상에서만 슬라이드 작동하도록 하기 (0) | 2024.11.26 |
---|---|
[Jquery]url로 페이지 구분해서 분기 처리하기 (0) | 2024.06.27 |
[jquery]태그안에 속성값 확인하여 중복되는 값을 가진 요소 삭제하기 (0) | 2024.05.21 |
[jquery]썸네일과 네이게이션버튼을 이미지로 연결하기 (0) | 2024.05.21 |
[jquery]window.location.href 이용하여 기능 개발 (0) | 2024.05.21 |