jQuery个性美化select下拉框 点击弹出层选择
XML/HTML Code
- <form action='#' method='post'>
- <fieldset>
- <select class="selectbox" id="" name="">
- <option value="freejs.net">freejs.net</option>
- <option value="CSS3">CSS3</option>
- <option value="Javascript">Javascript</option>
- <option value="Ruby">Ruby</option>
- <option value="Python">Python</option>
- <option value="C++">C++</option>
- </select>
- </fieldset>
- </form>
JavaScript Code
- <script type="text/javascript">
- $(document).ready(function (){
- $('.selectbox').wrap('<div class="select_wrapper"></div>')
- $('.selectbox').parent().prepend('<span>'+ $(this).find(':selected').text() +'</span>');
- $('.selectbox').parent().children('span').width($('.selectbox').width());
- $('.selectbox').css('display', 'none');
- $('.selectbox').parent().append('<ul class="select_inner"></ul>');
- $('.selectbox').children().each(function(){
- var opttext = $(this).text();
- var optval = $(this).val();
- $('.selectbox').parent().children('.select_inner').append('<li id="' + optval + '">' + opttext + '</li>');
- });
- $('.selectbox').parent().find('li').on('click', function (){
- var cur = $(this).attr('id');
- $('.selectbox').parent().children('span').text($(this).text());
- $('.selectbox').children().removeAttr('selected');
- $('.selectbox').children('[value="'+cur+'"]').attr('selected','selected');
- //alert($('.selectbox').children('[value="'+cur+'"]').text());
- /*console.log($('.selectbox').children('[value="'+cur+'"]').text());*/
- });
- $('.selectbox').parent().on('click', function (){
- $(this).find('ul').slideToggle('fast');
- });
- });
- </script>
原文地址:http://www.freejs.net/article_biaodan_657.html