Radio style

Trở về

  • 4,309

Đoạn code sau đây sẽ giúp chúng ta sử dụng các thành phần khác đối xử như một radio style.

Xử lý thành phần tương tự input radio

Html viết:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Học Web Chuẩn</title>
<script src="https://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
    $('li').click(function(){
        $('li').removeClass();
        $(this).addClass('checked');
    });
});
</script>
<style>
* { /* reset lại margin và padding cho các tag */
    margin: 0;
    padding: 0;
}
li {
    border: 1px solid blue;
    -moz-border-radius: 20px;
    -webkit-border-radius: 20px;
    -ms-border-radius: 20px;
    -o-border-radius: 20px;
    border-radius: 20px;
    cursor: pointer;
    float: left;
    list-style: none;
    margin-right: 5px;
    text-align: center;
    line-height: 30px;
    height: 30px;
    width: 30px;
}

.checked {
    background: #fff000;
}
</style>
</head>
<body>
<ul>
<li>01</li>
<li>02</li>
<li>03</li>
<li>04</li>
<li>05</li>
</ul>
</body>
</html>

Hiển thị trình duyệt:

Cách sử dụng trên tương tự như cách sử dụng checkbox sau đây:

So sánh code HTML trước và sau khi có jQuery:

Trước khi có jQuery Sau khi có jQuery

<ul>
<li>01</li>
<li>02</li>
<li>03</li>
<li>04</li>
<li>05</li>
</ul>

<ul>
<li>01</li>
<li class="checked">02</li>
<li>03</li>
<li>04</li>
<li>05</li>
</ul>

Download file để thực hành

Có một bình luận cho “Radio style”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.