如何在IE中使用JQuery隐藏和显示SELECT选项

前端之家收集整理的这篇文章主要介绍了如何在IE中使用JQuery隐藏和显示SELECT选项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图隐藏下拉菜单中的一些选项. JQuery的.hide()和.show()在Firefox和Chrome中运行良好,但在IE中没有运气.

有什么好主意吗?

解决方法

在许多可能的方法中,这种方法需要浏览器嗅探(通常不是很好),但不需要具有相同选择列表的多个副本来交换.
//To hide elements
$("select option").each(function(index,val){
    if ($(this).is('option') && (!$(this).parent().is('span')))
        $(this).wrap((navigator.appName == 'Microsoft Internet Explorer') ? '<span>' : null).hide();
});

//To show elements
$("select option").each(function(index,val) {
    if(navigator.appName == 'Microsoft Internet Explorer') {
        if (this.nodeName.toUpperCase() === 'OPTION') {
            var span = $(this).parent();
            var opt = this;
            if($(this).parent().is('span')) {
                $(opt).show();
                $(span).replaceWith(opt);
            }
        }
    } else {
        $(this).show(); //all other browsers use standard .show()
    }
});

对此的信任与Dima Svirid完全吻合:http://ajax911.com/hide-options-selecbox-jquery/

猜你在找的jQuery相关文章