Home >Web Front-end >JS Tutorial >jQuery implements custom drop-down list

jQuery implements custom drop-down list

PHPz
PHPzforward
2016-05-16 16:22:131107browse

This article mainly introduces the method and code sharing of using jQuery to implement a custom drop-down list. The effect is very good and the compatibility is also great. I recommend it to my friends here.

html code:

<p class="dropdownContainer">
    <p class="dropdownDefault">1</p>
    <span class="downArrow arrow"></span>
    <ul class="dropdrown-menu">
        <li><a href="#">1</a></li>
        <li><a href="#">2</a></li>
        <li><a href="#">6</a></li>
        <li><a href="#">8</a></li>
    </ul>
</p>

css code:

.dropdownContainer{position: relative;height: 30px;width: 100%;background: #555;}
.dropdownDefault{border:1px solid #ddd;line-height: 28px;text-indent: 0.5em;}
.dropdownContainer .downArrow{position: absolute;right: 5px;top: 9px}
.dropdrown-menu{position: absolute;top:100%;width: 100%;left: 0;background: #555;display: none;}
.dropdrown-menu li{line-height: 24px;}
.dropdrown-menu li a{display: inline-block;width: 100%;text-indent: 0.5em}
.dropdrown-menu li a:hover{background: #0078b6;font-size: 1.1em;}
.arrow{width: 0;height: 0;display: inline-block;cursor: pointer;}
.downArrow{border-left: 6px solid transparent;border-right: 6px solid transparent;border-top: 12px solid #fff;}

js (jquery) code:

$(".dropdownDefault,.dropdownContainer .downArrow").click(function(){
    $(this).siblings(".dropdrown-menu").show();
});
 
$(".dropdrown-menu li a").click(function(){
    $(this).parent().parent().siblings(".dropdownDefault").html($(this).html());
    $(this).parent().parent().hide();
});

Although the code is very simple, But isn’t the effect very cool?

The above is the entire content of this chapter. For more related tutorials, please visit jQuery Video Tutorial!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete