如何使用自定义外观列表在列表下拉列表中隐藏选择占位符

双腰带

当我在列表下拉菜单中打开它时,我无法隐藏第一个选项“选择一个选项...”。我怎样才能做到这一点?这是一个很好的选择示例。如何隐藏第一个选项或隐藏特定占位符“选择一个选项...”?

添加禁用或隐藏不起作用。

var numberOfSelects = $('select').length;

// Iterate over each select element
$('select').each( function() {
    
    // Cache the number of options
    var $this = $(this),
        numberOfOptions = $(this).children('option').length;
    
    // Hides the select element
    $this.addClass('hidden');
    
    // Wrap the select element in a div
    $this.wrap('<div class="select" />');
    
    // Insert a styled div to sit over the top of the hidden select element
    $this.after('<div class="styledSelect"></div>');
    
    // Cache the styled div
    var $styledSelect = $this.next('div.styledSelect');
    
    // Show the first select option in the styled div
    $styledSelect.text($this.children('option').eq(0).text());
    
    // Insert an unordered list after the styled div and also cache the list
    var $list = $('<ul />', {
        'class' : 'options'
    }).insertAfter($styledSelect);
    
    // Insert a list item into the unordered list for each select option
    for(var i = 0; i < numberOfOptions; i++) {
        $('<li />', {
            text: $this.children('option').eq(i).text()
        }).appendTo($list);
    }
    
    // Cache the list items
    var $listItems = $list.children('li');
    
    // Show the unordered list when the styled div is clicked (also hides it if the div is clicked again)
    $styledSelect.click( function(e) {
        e.stopPropagation();
        $('div.styledSelect.active').each( function() {
            $(this).removeClass('active')
                .next('ul.options').filter(':not(:animated)').slideUp(250);   
        });
        /* Use this instead of the .each() method when dealing with a large number of elements:
        for(var i = 0; i < numberOfSelects; i++) {
            if($('div.styledSelect').eq(i).hasClass('active') === true) {
                $('div.styledSelect').eq(i).removeClass('active')
                    .next('ul.options').filter(':not(:animated)').slideUp(250);
            }
        } */
        $(this).toggleClass('active')
            .next('ul.options').filter(':not(:animated)').slideToggle(250);
    });
    
    // Hides the unordered list when a list item is clicked and updates the styled div to show the selected list item
    // Updates the select element to have the value of the equivalent option
    $listItems.click( function(e) {
        e.stopPropagation();
        $styledSelect.text($(this).text())
            .removeClass('active');
        $this.val($(this).text().toLowerCase());
        $list.filter(':not(:animated)').slideUp(250);
    });
    
    // Hides the unordered list when clicking outside of it
    $(document).click( function() {
        $styledSelect.removeClass('active');
        $list.filter(':not(:animated)').slideUp(250);
    });
    
});
body {
    padding: 50px;
}
body > div {
    display: inline-block;
    margin-bottom: 20px;
    margin-right: 20px;
}
label {
    color: #444;
    display: inline-block;
    font: bold 16px/1.5 sans-serif;
}
ul {list-style-type:none;}
label:after {
    content: ':';
}
.hidden {
    visibility: hidden;
}
.select {
    cursor: pointer;
    display: inline-block;
    padding-right: 16px;
    position: relative;
    -webkit-user-select:none;
       -moz-user-select:none;
        -ms-user-select:none;
         -o-user-select:none;
            user-select:none;
            height:45px;
}
.styledSelect {
    background-color: #000;
    background-image: url('http://cdn3.iconfinder.com/data/icons/fatcow/16x16_0140/bullet_arrow_down.png'),
                      -webkit-linear-gradient(hsla(0,0%,100%,.1), hsla(0,0%,0%,.1));
    background-image: url('http://cdn3.iconfinder.com/data/icons/fatcow/16x16_0140/bullet_arrow_down.png'),
                         -moz-linear-gradient(hsla(0,0%,100%,.1), hsla(0,0%,0%,.1));
    background-image: url('http://cdn3.iconfinder.com/data/icons/fatcow/16x16_0140/bullet_arrow_down.png'),
                          -ms-linear-gradient(hsla(0,0%,100%,.1), hsla(0,0%,0%,.1));
    background-image: url('http://cdn3.iconfinder.com/data/icons/fatcow/16x16_0140/bullet_arrow_down.png'),
                           -o-linear-gradient(hsla(0,0%,100%,.1), hsla(0,0%,0%,.1));
    background-image: url('http://cdn3.iconfinder.com/data/icons/fatcow/16x16_0140/bullet_arrow_down.png'),
                              linear-gradient(hsla(0,0%,100%,.1), hsla(0,0%,0%,.1));
    background-position: 97% 60%;
    background-repeat: no-repeat;
    border-radius: 0px;
    bottom: 0;
    color: #fff;
    font: 14px/24px sans-serif;
    left: 0;
    padding: 10px 8px;
    position: absolute;
    right: 0;
    top: 0;
}
.styledSelect:hover {
    background-color: #000; color:#fff;
}
.styledSelect:active,
.styledSelect.active {
    background-color: #000; color:#fff;
}
.options {
    background: #000;
    border-radius: 0 0 3px 3px;

    color: #fff;
    display: none;
    font: 14px/24px sans-serif;
    left: 0px;
    margin: 0;
    padding: 0;
    position: absolute;
    right: 0px;
    top: 97%;
    z-index: 999;
}
.active + .options {
    z-index: 9999;
}
.options li {
    padding: 10px 10px;
}
.options li:last-child {
    border-radius: 0 0 0px 0px;
}
.options li:hover {
    background: #fff;
    color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="favouriteAnimal">
        <option value="">Select an option&hellip;</option>
        <option value="antelope">Antelope</option>
        <option value="dugong">Dugong</option>
        <option value="giraffe">Giraffe</option>
        <option value="koala">Koala</option>
        <option value="turkey">Turkey</option>
        <option value="wombat">Wombat</option>
    </select>

斯鲁克

var numberOfSelects = $('select').length;

// Iterate over each select element
$('select').each( function() {
    
    // Cache the number of options
    var $this = $(this),
        numberOfOptions = $(this).children('option').length;
    
    // Hides the select element
    $this.addClass('hidden');
    
    // Wrap the select element in a div
    $this.wrap('<div class="select" />');
    
    // Insert a styled div to sit over the top of the hidden select element
    $this.after('<div class="styledSelect"></div>');
    
    // Cache the styled div
    var $styledSelect = $this.next('div.styledSelect');
    
    // Show the first select option in the styled div
    $styledSelect.text($this.children('option').eq(0).text());
    
    // Insert an unordered list after the styled div and also cache the list
    var $list = $('<ul />', {
        'class' : 'options'
    }).insertAfter($styledSelect);
    
    // Insert a list item into the unordered list for each select option
    for(var i = 0; i < numberOfOptions; i++) {
        $('<li />', {
            text: $this.children('option').eq(i).text()
        }).appendTo($list);
    }
    
    // Cache the list items
    var $listItems = $list.children('li');
    
    // Show the unordered list when the styled div is clicked (also hides it if the div is clicked again)
    $styledSelect.click( function(e) {
        e.stopPropagation();
        $('div.styledSelect.active').each( function() {
            $(this).removeClass('active')
                .next('ul.options').filter(':not(:animated)').slideUp(250);   
        });
        /* Use this instead of the .each() method when dealing with a large number of elements:
        for(var i = 0; i < numberOfSelects; i++) {
            if($('div.styledSelect').eq(i).hasClass('active') === true) {
                $('div.styledSelect').eq(i).removeClass('active')
                    .next('ul.options').filter(':not(:animated)').slideUp(250);
            }
        } */
        $(this).toggleClass('active')
            .next('ul.options').filter(':not(:animated)').slideToggle(250);
    });
    
    // Hides the unordered list when a list item is clicked and updates the styled div to show the selected list item
    // Updates the select element to have the value of the equivalent option
    $listItems.click( function(e) {
        e.stopPropagation();
        $styledSelect.text($(this).text())
            .removeClass('active');
        $this.val($(this).text().toLowerCase());
        $list.filter(':not(:animated)').slideUp(250);
    });
    
    // Hides the unordered list when clicking outside of it
    $(document).click( function() {
        $styledSelect.removeClass('active');
        $list.filter(':not(:animated)').slideUp(250);
    });
    
});



$('.styledSelect').on('click',function(){

var elem = $(this)

var vtxt = elem.text()

elem.next('.options').find("li").show();

/* To hide the option which is selected*/
elem.next('.options').find("li:contains("+ vtxt +")").hide();

/* To hide only select an option label*/
elem.next('.options').find("li:first-child").hide();

 


})
body {
    padding: 50px;
}
body > div {
    display: inline-block;
    margin-bottom: 20px;
    margin-right: 20px;
}
label {
    color: #444;
    display: inline-block;
    font: bold 16px/1.5 sans-serif;
}
ul {list-style-type:none;}
label:after {
    content: ':';
}
.hidden {
    visibility: hidden;
}
.select {
    cursor: pointer;
    display: inline-block;
    padding-right: 16px;
    position: relative;
    -webkit-user-select:none;
       -moz-user-select:none;
        -ms-user-select:none;
         -o-user-select:none;
            user-select:none;
            height:45px;
}
.styledSelect {
    background-color: #000;
    background-image: url('http://cdn3.iconfinder.com/data/icons/fatcow/16x16_0140/bullet_arrow_down.png'),
                      -webkit-linear-gradient(hsla(0,0%,100%,.1), hsla(0,0%,0%,.1));
    background-image: url('http://cdn3.iconfinder.com/data/icons/fatcow/16x16_0140/bullet_arrow_down.png'),
                         -moz-linear-gradient(hsla(0,0%,100%,.1), hsla(0,0%,0%,.1));
    background-image: url('http://cdn3.iconfinder.com/data/icons/fatcow/16x16_0140/bullet_arrow_down.png'),
                          -ms-linear-gradient(hsla(0,0%,100%,.1), hsla(0,0%,0%,.1));
    background-image: url('http://cdn3.iconfinder.com/data/icons/fatcow/16x16_0140/bullet_arrow_down.png'),
                           -o-linear-gradient(hsla(0,0%,100%,.1), hsla(0,0%,0%,.1));
    background-image: url('http://cdn3.iconfinder.com/data/icons/fatcow/16x16_0140/bullet_arrow_down.png'),
                              linear-gradient(hsla(0,0%,100%,.1), hsla(0,0%,0%,.1));
    background-position: 97% 60%;
    background-repeat: no-repeat;
    border-radius: 0px;
    bottom: 0;
    color: #fff;
    font: 14px/24px sans-serif;
    left: 0;
    padding: 10px 8px;
    position: absolute;
    right: 0;
    top: 0;
}
.styledSelect:hover {
    background-color: #000; color:#fff;
}
.styledSelect:active,
.styledSelect.active {
    background-color: #000; color:#fff;
}
.options {
    background: #000;
    border-radius: 0 0 3px 3px;

    color: #fff;
    display: none;
    font: 14px/24px sans-serif;
    left: 0px;
    margin: 0;
    padding: 0;
    position: absolute;
    right: 0px;
    top: 97%;
    z-index: 999;
}
.active + .options {
    z-index: 9999;
}
.options li {
    padding: 10px 10px;
}
.options li:last-child {
    border-radius: 0 0 0px 0px;
}
.options li:hover {
    background: #fff;
    color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="favouriteAnimal">
        <option value="">Select an option&hellip;</option>
        <option value="antelope">Antelope</option>
        <option value="dugong">Dugong</option>
        <option value="giraffe">Giraffe</option>
        <option value="koala">Koala</option>
        <option value="turkey">Turkey</option>
        <option value="wombat">Wombat</option>
    </select>

希望这会奏效。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用静态占位符和自定义图标构建引导下拉列表

来自分类Dev

如何隐藏自定义选择列表中的选定选项?

来自分类Dev

表单下拉列表的占位符选择类型?

来自分类Dev

如何从自定义下拉列表中获取价值?

来自分类Dev

Laravel数据库使用占位符选择下拉列表

来自分类Dev

下拉列表中的MVC占位符

来自分类Dev

样式引导自定义选择下拉列表

来自分类Dev

在剑道网格的选择功能中选择自定义下拉列表中的项目

来自分类Dev

如何使用自定义数组适配器删除自定义列表中的特定列表项(列表行)?

来自分类Dev

如何使用自定义数组适配器删除自定义列表中的特定列表项(列表行)?

来自分类Dev

如何使用从MS Excel 2010中的自定义下拉列表中选择的值自动为单元格背景着色

来自分类Dev

在自定义指令中使用ng Repeat来填充选择下拉列表

来自分类Dev

如何制作水平/自定义Bootstrap下拉列表?

来自分类Dev

单击自定义时如何关闭JCombobox的下拉列表?

来自分类Dev

使下拉列表在自定义Excel功能区中工作

来自分类Dev

在Moodle中添加自定义用户字段(下拉列表)

来自分类Dev

Android Spinner下拉列表自定义

来自分类Dev

如何从自定义列表中获取不同的列表?

来自分类Dev

如何隐藏jquery选择的下拉列表框中的值?

来自分类Dev

rails select_tag,如何使用数组中的json值和自定义文本生成下拉列表

来自分类Dev

如何在使用sql数据源时在下拉列表中添加自定义项目

来自分类Dev

使用jQuery或JS在不同的下拉列表选择中隐藏div

来自分类Dev

如何让自定义字符串显示在form.collection_select的下拉列表中

来自分类Dev

如何在WebStorm中自定义此建议下拉列表

来自分类Dev

如何通过自定义加载项在 Outlook 会议请求中添加下拉列表?

来自分类Dev

如何在Flutter中使用自定义数据类型下拉列表按钮?

来自分类Dev

如何使用Rest API从自定义列表的文件夹中获取列表项?

来自分类Dev

自定义选择列表项

来自分类Dev

自定义选择列表项

Related 相关文章

  1. 1

    如何使用静态占位符和自定义图标构建引导下拉列表

  2. 2

    如何隐藏自定义选择列表中的选定选项?

  3. 3

    表单下拉列表的占位符选择类型?

  4. 4

    如何从自定义下拉列表中获取价值?

  5. 5

    Laravel数据库使用占位符选择下拉列表

  6. 6

    下拉列表中的MVC占位符

  7. 7

    样式引导自定义选择下拉列表

  8. 8

    在剑道网格的选择功能中选择自定义下拉列表中的项目

  9. 9

    如何使用自定义数组适配器删除自定义列表中的特定列表项(列表行)?

  10. 10

    如何使用自定义数组适配器删除自定义列表中的特定列表项(列表行)?

  11. 11

    如何使用从MS Excel 2010中的自定义下拉列表中选择的值自动为单元格背景着色

  12. 12

    在自定义指令中使用ng Repeat来填充选择下拉列表

  13. 13

    如何制作水平/自定义Bootstrap下拉列表?

  14. 14

    单击自定义时如何关闭JCombobox的下拉列表?

  15. 15

    使下拉列表在自定义Excel功能区中工作

  16. 16

    在Moodle中添加自定义用户字段(下拉列表)

  17. 17

    Android Spinner下拉列表自定义

  18. 18

    如何从自定义列表中获取不同的列表?

  19. 19

    如何隐藏jquery选择的下拉列表框中的值?

  20. 20

    rails select_tag,如何使用数组中的json值和自定义文本生成下拉列表

  21. 21

    如何在使用sql数据源时在下拉列表中添加自定义项目

  22. 22

    使用jQuery或JS在不同的下拉列表选择中隐藏div

  23. 23

    如何让自定义字符串显示在form.collection_select的下拉列表中

  24. 24

    如何在WebStorm中自定义此建议下拉列表

  25. 25

    如何通过自定义加载项在 Outlook 会议请求中添加下拉列表?

  26. 26

    如何在Flutter中使用自定义数据类型下拉列表按钮?

  27. 27

    如何使用Rest API从自定义列表的文件夹中获取列表项?

  28. 28

    自定义选择列表项

  29. 29

    自定义选择列表项

热门标签

归档