테이블 하이퍼링크 관련문의드려요.

테이블 하이퍼링크 관련문의드려요.

QA

테이블 하이퍼링크 관련문의드려요.

본문


<style>
.shop_submenu table {width:100%; max-width:600px;margin-top: 15px; border-bottom: 2px solid #575757;}
.shop_submenu table td {width:33%; 
    text-align: center;
    font-size: 1.4em;
    background: #e3e3e3;
    font-weight: 500;
    color: #4d4d4d;
}
.shop_submenu table td:hover {background:red; color:white;}
</style>
 
<div class="shop_submenu">
<table>
    <tr>
        <td onClick="location.href='/shop/list.php?ca_id=10'">주문1</td>
        <td onClick="location.href='/shop/list.php?ca_id=20'">주문2</td>
        <td onClick="location.href='/shop/list.php?ca_id=30'">주문3</td>
    </tr>
</table>

 

쇼핑몰 제품리스트 바로 위에 별도의 서브메뉴를 넣어 구분하고 싶습니다.

버튼 3개를 넣었는데.. 페이지 이동이라서 클릭 후 hover 색상이 유지가 안되네요.

페이지가 전환되더라도 누른버튼의 배경색을 유지시키는 방법 알 수 있을까요?

이 질문에 댓글 쓰기 :

답변 3


<style>
.shop_submenu table td .active {background:red; color:white;}
</style>
<div class="shop_submenu">
<table>
    <tr>
        <td class="<?php if($_GET['ca_id'] == 10) echo 'active'; ?>" onClick="location.href='/shop/list.php?ca_id=10'">주문1</td>
        <td class="<?php if($_GET['ca_id'] == 20) echo 'active'; ?>" onClick="location.href='/shop/list.php?ca_id=20'">주문2</td>
        <td class="<?php if($_GET['ca_id'] == 30) echo 'active'; ?>" onClick="location.href='/shop/list.php?ca_id=30'">주문3</td>
    </tr>
</table>

 

이렇게도 할수 있습니다.

아래의 코드를 참고해 보시겠어요?

 

PHP

 

<?php
// 현재 페이지의 ca_id를 얻어옴
$current_ca_id = isset($_GET['ca_id']) ? $_GET['ca_id'] : '';

// 버튼에 active 클래스를 추가하기 위한 배열
$button_classes = array(
    '10' => '',
    '20' => '',
    '30' => ''
);

if (array_key_exists($current_ca_id, $button_classes)) {
    $button_classes[$current_ca_id] = 'active';
}
?>
 

HTML CSS

 

<style>
.shop_submenu table {
    width: 100%;
    max-width: 600px;
    margin-top: 15px;
    border-bottom: 2px solid #575757;
}
.shop_submenu table td {
    width: 33%;
    text-align: center;
    font-size: 1.4em;
    background: #e3e3e3;
    font-weight: 500;
    color: #4d4d4d;
    cursor: pointer;
    transition: background 0.3s, color 0.3s;
}
.shop_submenu table td:hover {
    background: red;
    color: white;
}
.shop_submenu table td.active {
    background: red;
    color: white;
}
</style>

<div class="shop_submenu">
    <table>
        <tr>
            <td class="<?php echo $button_classes['10']; ?>" onClick="location.href='/shop/list.php?ca_id=10'">주문1</td>
            <td class="<?php echo $button_classes['20']; ?>" onClick="location.href='/shop/list.php?ca_id=20'">주문2</td>
            <td class="<?php echo $button_classes['30']; ?>" onClick="location.href='/shop/list.php?ca_id=30'">주문3</td>
        </tr>
    </table>
</div>
 

 

다음 코드가 도움이 될지 모르겠습니다.

 


<style>
.shop_submenu table {width:100%; max-width:600px;margin-top: 15px; border-bottom: 2px solid #575757;}
.shop_submenu table td {width:33%; 
    text-align: center;
    font-size: 1.4em;
    background: #e3e3e3;
    font-weight: 500;
    color: #4d4d4d;
}
.shop_submenu table td:where(:hover,.active) {background:red; color:white;}
</style>
<div class="shop_submenu">
<table>
    <tr>
        <td onClick="location.href='/shop/list.php?ca_id=10'">주문1</td>
        <td onClick="location.href='/shop/list.php?ca_id=20'">주문2</td>
        <td onClick="location.href='/shop/list.php?ca_id=30'">주문3</td>
    </tr>
</table>
 
<script>
var qs = new URLSearchParams(location.search);
var ca_id = qs.get('ca_id');
if (ca_id != null) {
    document.querySelectorAll('.shop_submenu table td').forEach((el, i) => {
        var param_each = el.getAttribute('onclick').match(/(?<=ca_id=)[^&$']+/);
        if (param_each != null && param_each[0] == ca_id) {
            el.classList.add('active');
        }
    });
}
</script>
답변을 작성하시기 전에 로그인 해주세요.
전체 7
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT