修复 IE7 中下拉菜单被截断

Avatar of Chris Coyier
Chris Coyier

加载 jQuery 后运行(至少运行下面的“使用”部分),可以在页面末尾或 DOM 就绪语句中运行。请注意,此修复程序会创建一个下拉菜单的克隆,该克隆会与表单数据一起提交,但名称值已在末尾添加了“ -clone”,因此请注意这一点,尤其是在您序列化所有输入时。

感谢 Craig Hoover。

// Safely use $
(function($) {

	$.fn._ie_select=function() { 
	
		return $(this).each(function() { 
		
			var a = $(this),
			    p = a.parent();
		
			p.css('position','relative');
		
			var o = a.position(),
			    h = a.outerHeight(),
			    l = o.left,
			    t = o.top;
		
			var c = a.clone(true);
		
			$.data(c,'element',a);
		
			c.css({
				zIndex   : 100,
				height   : h,
				top      : t,
				left     : l,
				position : 'absolute',
				width    : 'auto',
				opacity  : 0
			}).attr({
				id    : this.id + '-clone',
				name  : this.name + '-clone'
			}).change(function() {
				$.data(c,'element')
					.val($(this).val())
					.trigger('change')
			});
				
			a.before(c).click(function() { 
				c.trigger('click');
			});
		
		}); // END RETURN
	
	}; // END PLUGIN
        
        // Usage
	if ($.browser.msie) {
		$('select')._ie_select();
	}

})(jQuery); // END SAFETY