답변 2개 / 댓글 6개
채택된 답변
+20 포인트
구체적으로 소스코드가 어떻게 되어 있는지는 알수 없지만
예를 들어 다음과 같이 해 볼 수 있을것 같습니다.
예제를 참고하셔서 원하시는 로직으로 구현 하시면 되지 않을까 합니다.
Copy
<button id="recommendButton">추천</button>
<div id="recommendationMessage"></div>
<script src="recommendation.js"></script>
Copy
document.addEventListener("DOMContentLoaded", function() {
var recommendButton = document.getElementById("recommendButton");
var recommendationMessage = document.getElementById("recommendationMessage");
recommendButton.addEventListener("click", function() {
recommendationMessage.textContent = "추천을 했습니다.";
});
});
답변에 대한 댓글 6개
다음을 참고하셔서 view 파일에 적용하시면 되지 않을까 생각합니다.
[code]
<?php if ($board['bo_use_nogood']) { // 비추천 ?>
<button type="button" onclick="na_good('<?php echo $bo_table ?>', '<?php echo $wr_id ?>', 'nogood', 'wr_nogood');" class="btn btn-basic" title="비추천">
<i class="fa fa-thumbs-o-down" aria-hidden="true"></i>
<b id="추천완료">추천하기</b>
<span class="sr-only">비추천</span>
</button>
<script>
// JavaScript 코드 추가
function na_good(bo_table, wr_id, type, target) {
// 추천이 완료되면 메시지를 표시하는 함수
var recommendationMessage = document.createElement("div");
recommendationMessage.textContent = "추천을 했습니다.";
document.getElementById(target).appendChild(recommendationMessage);
// 여기에 na_good 함수의 나머지 로직을 추가할 수 있습니다.
// na_good 함수는 이미 정의되어 있다고 가정합니다.
}
</script>
<?php } ?>
[/code]
[code]
<?php if ($board['bo_use_nogood']) { // 비추천 ?>
<button type="button" onclick="na_good('<?php echo $bo_table ?>', '<?php echo $wr_id ?>', 'nogood', 'wr_nogood');" class="btn btn-basic" title="비추천">
<i class="fa fa-thumbs-o-down" aria-hidden="true"></i>
<b id="추천완료">추천하기</b>
<span class="sr-only">비추천</span>
</button>
<script>
// JavaScript 코드 추가
function na_good(bo_table, wr_id, type, target) {
// 추천이 완료되면 메시지를 표시하는 함수
var recommendationMessage = document.createElement("div");
recommendationMessage.textContent = "추천을 했습니다.";
document.getElementById(target).appendChild(recommendationMessage);
// 여기에 na_good 함수의 나머지 로직을 추가할 수 있습니다.
// na_good 함수는 이미 정의되어 있다고 가정합니다.
}
</script>
<?php } ?>
[/code]
JavaScript 코드에서 document.getElementById(target)를 사용하여 해당 요소를 찾을 수 없기 때문에 발생하는 오류 인것 같습니다.
다음과 같이 수정해 보세요
[code]
function na_good(bo_table, wr_id, type, target) {
// 해당 요소를 찾습니다.
var targetElement = document.getElementById(target);
// 요소가 존재하는지 확인합니다.
if (targetElement) {
// 추천이 완료되면 메시지를 표시하는 함수
var recommendationMessage = document.createElement("div");
recommendationMessage.textContent = "추천을 했습니다.";
targetElement.appendChild(recommendationMessage);
} else {
console.error("Element with ID '" + target + "' not found.");
}
// 여기에 na_good 함수의 나머지 로직을 추가할 수 있습니다.
// na_good 함수는 이미 정의되어 있다고 가정합니다.
}
[/code]
다음과 같이 수정해 보세요
[code]
function na_good(bo_table, wr_id, type, target) {
// 해당 요소를 찾습니다.
var targetElement = document.getElementById(target);
// 요소가 존재하는지 확인합니다.
if (targetElement) {
// 추천이 완료되면 메시지를 표시하는 함수
var recommendationMessage = document.createElement("div");
recommendationMessage.textContent = "추천을 했습니다.";
targetElement.appendChild(recommendationMessage);
} else {
console.error("Element with ID '" + target + "' not found.");
}
// 여기에 na_good 함수의 나머지 로직을 추가할 수 있습니다.
// na_good 함수는 이미 정의되어 있다고 가정합니다.
}
[/code]
답변을 작성하려면 로그인이 필요합니다.
<button type="button" onclick="na_good('<?php echo $bo_table ?>', '<?php echo $wr_id ?>', 'nogood', 'wr_nogood');" class="btn btn-basic" title="비추천">
<i class="fa fa-thumbs-o-down" aria-hidden="true"></i>
<b id="추천완료">추천하기</b>
<span class="sr-only">비추천</span>
</button>
<?php } ?>
추천 버튼은 이렇게 되어 있습니다~