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

我试图隐藏下拉菜单中的一些选项。 JQuery的.show() .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') ? '' : 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/

只是提到IE11 navigator.appName返回’Netscape’:)所以考虑到它:

 $("select option[value='your_option_value']").wrap((navigator.appName == 'Microsoft Internet Explorer' || navigator.appName == 'Netscape') ? '' : null).hide();