我试图隐藏下拉菜单中的一些选项.
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/