如何在IE中使用JQuery隐藏和显示SELECT选项 [英] How to Hide and Show SELECT Options with JQuery in IE
本文介绍了如何在IE中使用JQuery隐藏和显示SELECT选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从下拉菜单中隐藏一些选项. jQuery的.hide()
和.show()
在Firefox和Chrome中运行良好,但在IE中没有运气.
I'm trying to hide some options from a drop down. JQuery's .hide()
and .show()
work great in Firefox and Chrome, but no luck in IE.
有什么好主意吗?
推荐答案
在许多可能的方法中,此方法需要浏览器嗅探(通常不太好),但不需要具有相同选择列表的多个副本即可交换进进出出.
Of many possible approaches, this method requires browser sniffing (which in general is not great) but doesn't require having multiple copies of the same select list to swap in and out.
//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/
Credit for this lies squarely with Dima Svirid here: http://ajax911.com/hide-options-selecbox-jquery/
这篇关于如何在IE中使用JQuery隐藏和显示SELECT选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文