IE 中下拉列表的宽度
-
09-06-2019 - |
题
在 IE 中,下拉列表采用与保管箱相同的宽度(我希望我说得通),而在 Firefox 中,下拉列表的宽度根据内容而变化。
这基本上意味着我必须确保保管箱足够宽以显示最长的选择。这使我的页面看起来非常难看:(
有解决这个问题的方法吗?如何使用 CSS 为 dropbox 和 dropdownlist 设置不同的宽度?
解决方案
这是另一个 jQuery 基于示例。与此处发布的所有其他答案相反,它考虑了所有键盘和鼠标事件,特别是点击:
if (!$.support.leadingWhitespace) { // if IE6/7/8
$('select.wide')
.bind('focus mouseover', function() { $(this).addClass('expand').removeClass('clicked'); })
.bind('click', function() { $(this).toggleClass('clicked'); })
.bind('mouseout', function() { if (!$(this).hasClass('clicked')) { $(this).removeClass('expand'); }})
.bind('blur', function() { $(this).removeClass('expand clicked'); });
}
与这段 CSS 结合使用:
select {
width: 150px; /* Or whatever width you want. */
}
select.expand {
width: auto;
}
您需要做的就是添加类 wide
到有问题的下拉元素。
<select class="wide">
...
</select>
这是一个 jsfiddle 示例. 。希望这可以帮助。
其他提示
创建您自己的下拉列表是一件痛苦的事情,而不是值得的事情。您可以使用一些 JavaScript 来使 IE 下拉菜单正常工作。
它使用了一些 YUI 库和一个特殊的扩展来修复 IE 选择框。
您将需要包括以下内容并包装您的 <select>
中的元素 <span class="select-box">
将它们放在页面的正文标记之前:
<script src="http://us.js2.yimg.com/us.js.yimg.com/lib/common/utils/2/yahoo_2.0.0-b3.js" type="text/javascript">
</script>
<script src="http://us.js2.yimg.com/us.js.yimg.com/lib/common/utils/2/event_2.0.0-b3.js" type="text/javascript">
</script>
<script src="http://us.js2.yimg.com/us.js.yimg.com/lib/common/utils/2/dom_2.0.2-b3.js" type="text/javascript">
</script>
<script src="ie-select-width-fix.js" type="text/javascript">
</script>
<script>
// for each select box you want to affect, apply this:
var s1 = new YAHOO.Hack.FixIESelectWidth( 's1' ); // s1 is the ID of the select box you want to affect
</script>
验收后编辑:
您也可以在没有 YUI 库和 Hack 控件的情况下执行此操作。您真正需要做的就是在选择元素上放置一个 onmouseover="this.style.width='auto'" onmouseout="this.style.width='100px'" (或任何您想要的)。YUI 控件给它提供了漂亮的动画,但这不是必需的。此任务也可以使用 jquery 和其他库来完成(尽管我还没有找到这方面的明确文档)
——修改编辑:
IE 对于选择控件的 onmouseout 存在问题(它不认为选项上的鼠标悬停是选择上的鼠标悬停)。这使得使用鼠标移出变得非常棘手。第一个解决方案是迄今为止我发现的最好的解决方案。
你可以尝试以下操作...
styleClass="someStyleWidth"
onmousedown="javascript:if(navigator.appName=='Microsoft Internet Explorer'){this.style.position='absolute';this.style.width='auto'}"
onblur="this.style.position='';this.style.width=''"
我尝试过,它对我有用。不需要其他任何东西。
我使用了以下解决方案,它似乎在大多数情况下都运行良好。
<style>
select{width:100px}
</style>
<html>
<select onmousedown="if($.browser.msie){this.style.position='absolute';this.style.width='auto'}" onblur="this.style.position='';this.style.width=''">
<option>One</option>
<option>Two - A long option that gets cut off in IE</option>
</select>
</html>
笔记:这 $.browser.msie 确实需要jquery。
@Thad你还需要添加一个模糊事件处理程序
$(document).ready(function(){
$("#dropdown").mousedown(function(){
if($.browser.msie) {
$(this).css("width","auto");
}
});
$("#dropdown").change(function(){
if ($.browser.msie) {
$(this).css("width","175px");
}
});
$("#dropdown").blur(function(){
if ($.browser.msie) {
$(this).css("width","175px");
}
});
});
但是,这仍然会在单击时展开选择框,而不仅仅是元素。(在 IE6 中似乎失败,但在 Chrome 和 IE7 中完美运行)
在IE6/IE7/IE8中没有办法做到这一点。该控件由应用程序绘制,而 IE 根本不会以这种方式绘制它。如果下拉菜单的一个宽度和列表的另一个宽度非常重要,那么最好的选择是通过简单的 HTML/CSS/JavaScript 实现您自己的下拉菜单。
如果您使用 jQuery,请尝试这个 IE 选择宽度插件:
http://www.jainaewen.com/files/javascript/jquery/ie-select-style/
应用此插件使 Internet Explorer 中的选择框看起来像在 Firefox、Opera 等中一样工作,允许选项元素以全宽打开,而不会丢失固定宽度的外观和样式。它还添加了对 Internet Explorer 6 和 7 中选择框的填充和边框的支持。
在 jQuery 中,这工作得相当好。假设下拉菜单的 id="dropdown"。
$(document).ready(function(){
$("#dropdown").mousedown(function(){
if($.browser.msie) {
$(this).css("width","auto");
}
});
$("#dropdown").change(function(){
if ($.browser.msie) {
$(this).css("width","175px");
}
});
});
这是最简单的解决方案。
在开始之前,我必须告诉您下拉选择框在除 IE6 之外的几乎所有浏览器中都会自动展开。因此,我会进行浏览器检查(即 IE6)并仅将以下内容写入该浏览器。就这样吧。首先检查浏览器。
该代码将神奇地展开下拉选择框。该解决方案的唯一问题是鼠标悬停时下拉列表将扩展为 420px,并且由于溢出 = 隐藏,我们隐藏扩展的下拉列表大小并将其显示为 170px;这样,ddl右侧的箭头就会被隐藏起来,看不到。但选择框会扩大到420px;这才是我们真正想要的。您只需亲自尝试下面的代码,如果您喜欢就可以使用它。
.ctrDropDown
{
width:420px; <%--this is the actual width of the dropdown list--%>
}
.ctrDropDownClick
{
width:420px; <%-- this the width of the dropdown select box.--%>
}
<div style="width:170px; overflow:hidden;">
<asp:DropDownList runat="server" ID="ddlApplication" onmouseout = "this.className='ctrDropDown';" onmouseover ="this.className='ctrDropDownClick';" class="ctrDropDown" onBlur="this.className='ctrDropDown';" onMouseDown="this.className='ctrDropDownClick';" onChange="this.className='ctrDropDown';"></asp:DropDownList>
</div>
以上是IE6的CSS。所有其他浏览器的通用 CSS 应该如下所示。
.ctrDropDown
{
width:170px; <%--this is the actual width of the dropdown list--%>
}
.ctrDropDownClick
{
width:auto; <%-- this the width of the dropdown select box.--%>
}
如果你想要一个简单的下拉菜单和/或弹出菜单,没有过渡效果,只需使用 CSS...您可以使用 .htc 文件(css3hover?)强制 IE6 支持所有元素上的 :hover,并在有条件附加的 CSS 文件中定义行为(仅限 IE6 属性)。
看一下这个..它并不完美,但它可以工作,并且仅适用于 IE,不会影响 FF。我使用 onmousedown 的常规 javascript 来建立仅 IE 修复..但是 jquery 中的 msie 也可以在 onmousedown 中使用。主要思想是“onchange”和“onblur”以使选择框恢复正常......决定你自己的宽度。我需要 35%。
onmousedown="javascript:if(navigator.appName=='Microsoft Internet Explorer'){this.style.width='auto'}"
onchange="this.style.width='35%'"
onblur="this.style.width='35%'"
BalusC 的上述答案效果很好,但是如果下拉列表的内容的宽度比您在 CSS select.expand 中定义的宽度小,我会添加一个小修复,将其添加到鼠标悬停绑定中:
.bind('mouseover', function() { $(this).addClass('expand').removeClass('clicked');
if ($(this).width() < 300) // put your desired minwidth here
{
$(this).removeClass('expand');
}})
这是我从别人的东西中获取一些东西所做的事情。
$(document).ready(function () {
if (document.all) {
$('#<%=cboDisability.ClientID %>').mousedown(function () {
$('#<%=cboDisability.ClientID %>').css({ 'width': 'auto' });
});
$('#<%=cboDisability.ClientID %>').blur(function () {
$(this).css({ 'width': '208px' });
});
$('#<%=cboDisability.ClientID %>').change(function () {
$('#<%=cboDisability.ClientID %>').css({ 'width': '208px' });
});
$('#<%=cboEthnicity.ClientID %>').mousedown(function () {
$('#<%=cboEthnicity.ClientID %>').css({ 'width': 'auto' });
});
$('#<%=cboEthnicity.ClientID %>').blur(function () {
$(this).css({ 'width': '208px' });
});
$('#<%=cboEthnicity.ClientID %>').change(function () {
$('#<%=cboEthnicity.ClientID %>').css({ 'width': '208px' });
});
}
});
其中 cboEthnicity 和 cboDisability 是选项文本比选择本身宽度更宽的下拉菜单。
如您所见,我指定了 document.all,因为这只适用于 IE。另外,我将下拉菜单包含在 div 元素中,如下所示:
<div id="dvEthnicity" style="width: 208px; overflow: hidden; position: relative; float: right;"><asp:DropDownList CssClass="select" ID="cboEthnicity" runat="server" DataTextField="description" DataValueField="id" Width="200px"></asp:DropDownList></div>
当您的下拉菜单展开时,这可以解决其他元素移出位置的问题。这里唯一的缺点是,当您选择时,菜单列表视觉效果会消失,但在您选择后会立即返回。
希望这对某人有帮助。
这是最好的方法:
select:focus{
min-width:165px;
width:auto;
z-index:9999999999;
position:absolute;
}
它与 BalusC 解决方案完全相同。只有这样才比较容易。;)
一个完整的 jQuery 插件可用。它支持不间断的布局和键盘交互,查看演示页面: http://powerkiki.github.com/ie_expand_select_width/
免责声明:我编码了那个东西,欢迎补丁
jquery BalusC的解决方案是我改进的。还使用:布拉德·罗伯逊的 在这里发表评论.
只需将其放入 .js 中,使用您想要的组合的宽类,并且不要伪造给它一个 Id。在 onload(或 documentReady 或其他)中调用该函数。
就这么简单:)
它将使用您为组合定义的宽度作为最小长度。
function fixIeCombos() {
if ($.browser.msie && $.browser.version < 9) {
var style = $('<style>select.expand { width: auto; }</style>');
$('html > head').append(style);
var defaultWidth = "200";
// get predefined combo's widths.
var widths = new Array();
$('select.wide').each(function() {
var width = $(this).width();
if (!width) {
width = defaultWidth;
}
widths[$(this).attr('id')] = width;
});
$('select.wide')
.bind('focus mouseover', function() {
// We're going to do the expansion only if the resultant size is bigger
// than the original size of the combo.
// In order to find out the resultant size, we first clon the combo as
// a hidden element, add to the dom, and then test the width.
var originalWidth = widths[$(this).attr('id')];
var $selectClone = $(this).clone();
$selectClone.addClass('expand').hide();
$(this).after( $selectClone );
var expandedWidth = $selectClone.width()
$selectClone.remove();
if (expandedWidth > originalWidth) {
$(this).addClass('expand').removeClass('clicked');
}
})
.bind('click', function() {
$(this).toggleClass('clicked');
})
.bind('mouseout', function() {
if (!$(this).hasClass('clicked')) {
$(this).removeClass('expand');
}
})
.bind('blur', function() {
$(this).removeClass('expand clicked');
})
}
}
您可以直接向 select 元素添加样式:
<select name="foo" style="width: 200px">
因此该选择项的宽度将为 200 像素。
或者,您可以将类或 ID 应用于元素并在样式表中引用它
到目前为止还没有一个。不了解 IE8,但不能在 IE6 和 IE7 中完成,除非您使用 javascript 实现自己的下拉列表功能。网上有一些如何做到这一点的示例,尽管我认为复制现有功能没有多大好处。
我们在 asp:dropdownlist 上有同样的东西:
在 Firefox(3.0.5) 中,下拉列表是下拉列表中最长项目的宽度,大约是 600 像素宽或类似的东西。
这似乎适用于 IE6,并且不会破坏其他浏览器。另一个好处是,一旦您更改下拉选择,它就会自动更改菜单。
$(document).ready(function(){
$("#dropdown").mouseover(function(){
if($.browser.msie) {
$(this).css("width","auto");
}
});
$("#dropdown").change(function(){
if ($.browser.msie) {
$("#dropdown").trigger("mouseover");
}
});
});
第一个最佳答案中的hedgerwow链接(YUI动画解决方法)已损坏,我猜域名已过期。我在代码过期之前复制了该代码,因此您可以在这里找到它(代码所有者可以让我知道我再次上传是否侵犯了任何版权)
在同一篇博客文章中,我写了关于使用 YUI 按钮菜单制作与普通元素完全相同的 SELECT 元素。看看这是否有帮助,请告诉我!
基于发布的解决方案 赛伊, ,这就是使用 jQuery 的方法。
$(document).ready(function() {
if ($.browser.msie) $('select.wide')
.bind('onmousedown', function() { $(this).css({position:'absolute',width:'auto'}); })
.bind('blur', function() { $(this).css({position:'static',width:''}); });
});
我想我应该参加比赛。我制作了一个 SaaS 应用程序,并在表格中嵌入了一个选择菜单。这种方法有效,但它扭曲了表中的所有内容。
onmousedown="if(navigator.appName=='Microsoft Internet Explorer'){this.style.position='absolute';this.style.width='auto'}
onblur="if(navigator.appName=='Microsoft Internet Explorer'){this.style.position=''; this.style.width= '225px';}"
因此,为了让一切变得更好,我所做的就是将选择放入 z 索引的 div 中。
<td valign="top" style="width:225px; overflow:hidden;">
<div style="position: absolute; z-index: 5;" onmousedown="var select = document.getElementById('select'); if(navigator.appName=='Microsoft Internet Explorer'){select.style.position='absolute';select.style.width='auto'}">
<select name="select_name" id="select" style="width: 225px;" onblur="if(navigator.appName=='Microsoft Internet Explorer'){this.style.position=''; this.style.width= '225px';}" onChange="reportFormValues('filter_<?=$job_id?>','form_values')">
<option value="0">All</option>
<!--More Options-->
</select>
</div>
</td>
它在所有版本的 IE、Chrome、FF 和 Safari 中进行了测试
// JavaScript 代码
<script type="text/javascript">
<!-- begin hiding
function expandSELECT(sel) {
sel.style.width = '';
}
function contractSELECT(sel) {
sel.style.width = '100px';
}
// end hiding -->
</script>
// html代码
<select name="sideeffect" id="sideeffect" style="width:100px;" onfocus="expandSELECT(this);" onblur="contractSELECT(this);" >
<option value="0" selected="selected" readonly="readonly">Select</option>
<option value="1" >Apple</option>
<option value="2" >Orange + Banana + Grapes</option>
我必须解决这个问题,并且曾经想出了一个非常完整且可扩展的解决方案,适用于 IE6、7 和 8(并且显然与其他浏览器兼容)。我在这里写了一篇关于它的整篇文章: http://www.edgeoftheworld.fr/wp/work/dealing-with-fixed-sized-dropdown-lists-in-internet-explorer
我想我会为那些仍然遇到这个问题的人分享这个,因为上述解决方案在所有情况下都不起作用(在我看来)。
我尝试了所有这些解决方案,但没有一个完全适合我。这就是我想出的
$(document).ready(function () {
var clicknum = 0;
$('.dropdown').click(
function() {
clicknum++;
if (clicknum == 2) {
clicknum = 0;
$(this).css('position', '');
$(this).css('width', '');
}
}).blur(
function() {
$(this).css('position', '');
$(this).css('width', '');
clicknum = 0;
}).focus(
function() {
$(this).css('position', 'relative');
$(this).css('width', 'auto');
}).mousedown(
function() {
$(this).css('position', 'relative');
$(this).css('width', 'auto');
});
})(jQuery);
确保为 html 中的每个下拉列表添加一个下拉列表类
这里的技巧是使用专门的点击功能(我在这里找到它 每次使用 jQuery 选择 DropDownList 项目时都会触发事件)。这里的许多其他解决方案都使用事件处理程序更改,效果很好,但如果用户选择与之前选择的选项相同的选项,则不会触发。
与许多其他解决方案一样,焦点和鼠标按下适用于用户将下拉列表置于焦点时,模糊适用于用户单击离开时。
您可能还想在其中添加某种浏览器检测,以便它只影响 ie。不过在其他浏览器中看起来还不错